# 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();
	}
}
```

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LequctB1Ev-IjFgcGYn%2F-LequwftIBvOx1HUeiop%2FScreenshot%202019-05-14%20at%208.47.16%20PM.png?alt=media\&token=f22f8301-df56-49ab-8104-48e66573bba1)

#### 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 ?

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUddl35Z67lLAl_X8dN%2Fimage.png?alt=media\&token=48738e52-9ad5-4825-9ab0-bf72989a6c09)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUddpokfRjWp13VYPPd%2Fimage.png?alt=media\&token=86fd9633-144e-41d9-a195-73474c117c28)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUde-RPrG787E6hn8s_%2Fimage.png?alt=media\&token=b6d27213-028b-42f4-b1fe-bb1c195f72d3)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUde9gC9gp8wFWYUi0S%2Fimage.png?alt=media\&token=3cb0bc24-60af-494e-a281-504965f230b2)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUdeEjh2cspWZnIOcxe%2Fimage.png?alt=media\&token=63c005e9-87bf-4a4a-ade8-d8196d06a6d5)

### Why casting is needed ?

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LUddWvrmlmlVTBtkXzg%2F-LUdeU-dNur9QkkDxuzI%2Fimage.png?alt=media\&token=d7f30211-2cd8-49d0-b11d-39c8e39c67b9)

### 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();
		}
	}
}
```

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-Leqk8-CsA2vDnCwwcPi%2F-LeqkJB4oGNH42mUwb-a%2FScreenshot%202019-05-14%20at%208.01.10%20PM.png?alt=media\&token=e01c2d15-0572-47e0-b9b3-388a65ad60a9)
