27) Casting

Instance Of :

Example 1:

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:

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 :

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

Solution :

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 ?

Why casting is needed ?

Problems :

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

Last updated