Tuesday, September 29, 2015

Instanceof operator-3

instanceof operator-3

Understanding Real use of instanceof operator

Let's see the real use of instanceof keyword by the example given below.

    interface Printable{} 
    class A implements Printable{ 
    public void a(){System.out.println("a method");} 
    } 
    class B implements Printable{ 
    public void b(){System.out.println("b method");} 
    } 
     
    class Call{ 
    void invoke(Printable p){//upcasting 
    if(p instanceof A){ 
    A a=(A)p;//Downcasting  
    a.a(); 
    } 
    if(p instanceof B){ 
    B b=(B)p;//Downcasting  
    b.b(); 
    } 
     
    } 
    }//end of Call class 
     
    class Test{ 
    public static void main(String args[]){ 
    Printable p=new B(); 
    Call c=new Call(); 
    c.invoke(p); 
    } 
    } 

Output: b method

No comments:

Post a Comment