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);
}
}
it is b method
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.