instanceof operator, Understanding Real use of instanceof in java

Java Programming Language / Operators in java

977

Program:


interface Atnyla{}

class A implements Atnyla{
	public void a(){
		System.out.println("it is a method");
		}
}

class B implements Atnyla{
	public void b(){
		System.out.println("it is b method");
		}
}

class Call{
	void invoke(Atnyla p){//upcasting

	if(p instanceof A){
	A obj=(A)p;//Downcasting
	obj.a();
	}

	if(p instanceof B){
	B obj1=(B)p;//Downcasting
	obj1.b();
	}

  }
}//end of Call class

class InstanceOf{
	public static void main(String args[]){
	Atnyla p=new B();
	Call c=new Call();
	c.invoke(p);
	}
}

  

Output:

it is b method
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.