> For the complete documentation index, see [llms.txt](https://gyansetu-java.gitbook.io/core-java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-java.gitbook.io/core-java/casting.md).

# 27) Casting

### Instance Of :

#### Example 1:

```java
package com.gs.interfacePrac;

class Par1 {

}

class Child1 extends Par1 {

}

public class TestInstanceOf {

	public static void main(String[] args) {

		Par1 par1 = new Child1();
		Child1 child1 = new Child1();

		if (par1 instanceof Child1) {
			System.out.println("par1 instance of Child");
		}

		if (child1 instanceof Par1) {
			System.out.println("child1 instance of Parent");
		}

	}
}
```

```
par1 instance of Child
child1 instance of Parent
```

#### Example 2:

```java
package com.gs.interfacePrac;

class GrandPar {

}

class Par1 extends GrandPar {

}

class Child1 extends Par1 {

}

public class TestInstanceOf {

	public static void main(String[] args) {

		Par1 par1 = new Child1();
		Child1 child1 = new Child1();

		if (par1 instanceof Child1) {
			System.out.println("par1 instance of Child");
		}

		if (child1 instanceof Par1) {
			System.out.println("child1 instance of Parent");
		}

		if (child1 instanceof GrandPar) {
			System.out.println("child1 instance of Grand Parent");
		}

	}
}

```

```
par1 instance of Child
child1 instance of Parent
child1 instance of Grand Parent
```

### Casting :

```java
package com.gs.interfacePrac;

class Interviewer {

	void conductInterview() {
		System.out.println("Default");
	}

}

class Manager extends Interviewer {

	void conductInterview() {
		System.out.println("Manager is conducting");
	}

	void m1() {
		System.out.println("m1 man");
	}
}

class HRExecutive extends Interviewer {

	void conductInterview() {
		System.out.println("HR is conducting");
	}
}

public class TestCasting {
	public static void main(String[] args) {
		Interviewer interviewer = new Manager();
		// Manager manager = interviewer; ?? can we write this ?
		Manager manager = (Manager) interviewer;
		manager.m1();
		manager.conductInterview();
		
		Interviewer interviewer1 = new HRExecutive();
		Manager manager1 = (Manager) interviewer1;
		manager1.m1();
		manager1.conductInterview();
	}
}
```

![](/files/-LequwftIBvOx1HUeiop)

#### Solution :

```java
public class TestCasting {
	public static void main(String[] args) {
		Interviewer interviewer = new Manager();
		// Manager manager = interviewer; ?? can we write this ?
		Manager manager = (Manager) interviewer;
		manager.m1();
		manager.conductInterview();
		
		Interviewer interviewer1 = new HRExecutive();
		if(interviewer1 instanceof Manager) {
			Manager manager1 = (Manager) interviewer1;
			manager1.m1();
			manager1.conductInterview();			
		}
	}
}
```

### What is casting ?

![](/files/-LUddl35Z67lLAl_X8dN)

![](/files/-LUddpokfRjWp13VYPPd)

![](/files/-LUde-RPrG787E6hn8s_)

![](/files/-LUde9gC9gp8wFWYUi0S)

![](/files/-LUdeEjh2cspWZnIOcxe)

### Why casting is needed ?

![](/files/-LUdeU-dNur9QkkDxuzI)

### Problems :

```java
package com.gs.interfacePrac;

class Interviewer {

	void conductInterview() {
		System.out.println("Default");
	}

}

class Manager extends Interviewer {

	void conductInterview() {
		System.out.println("Manager is conducting");
	}

	void m1() {
		System.out.println("m1 man");
	}
}

class HRExecutive extends Interviewer {

	void conductInterview() {
		System.out.println("HR is conducting");
	}
}

public class TestCasting {
	public static void main(String[] args) {

		Interviewer[] interviewers = new Interviewer[2];

		interviewers[0] = new Manager();
		interviewers[1] = new HRExecutive();

		for (Interviewer interviewer : interviewers) {
			interviewer.conductInterview();
			((Manager)interviewer).m1();
		}
	}
}
```

![](/files/-LeqkJB4oGNH42mUwb-a)
