Home / Programs / instanceof operator, Understanding Real use of instanceof in java
🚀 Programming Example

instanceof operator, Understanding Real use of instanceof in java

👁 1,085 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code


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

  
                        

🖥 Program Output

it is b method
Press any key to continue . . .
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.