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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-java.gitbook.io/core-java/casting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
