instanceOf keyword in JAVA
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2551337/instanceof-incompatible-conditional-operand-types">instanceof - incompatible conditional operand types</a>  </p>
</blockquote>



<p>I am testing JAVA "instance of" operator for brush up my mind. I see that the keyword is used to check if a reference is instance of a class or not.</p>

<p>But we use a reference to compare with a class that is not having any IS-A relationship with the class then it gives compile time error.</p>

<p>See the following code :</p>

<pre><code>package instanceofdemo;
public class Sample_1 {

    public static void main(String a[]){
        A iface = new Subclass();

        ///HERE INTERFACE OBJECT IS DOWNCASTED TO SUBCLASS
        if(iface instanceof  SuperClass){
            System.out.println("iface instanceof  SuperClass");
        }

        ///HERE INTERFACE OBJECT IS DOWNCASTED TO SUBCLASS
        if(iface instanceof  Subclass){
            System.out.println("iface instanceof  Subclass");
        }

        if(iface instanceof  A){
            System.out.println("iface instanceof  A");
        }


        Subclass sub = new Subclass();

        //SO INSTANCE OF ONLY WORKS WITH IS-A RELATION SHIP IN BI-DIRECTIONAL WAY
        //IT WILL GIVE COMPILE TIME ERROR, IF YOU TRY TO USE INSTANCE-OF WITH NON-RELATED CLASS
        if(sub instanceof  Independent){

        }

    }

}

interface  A{

}


class SuperClass implements A {

}



class Subclass extends  SuperClass{

}

class Independent{

}
</code></pre>

<p>In above code : when it gives compilation error at <code>if(iface instance of Independent)</code> line bcoz iface is not in "IS-A" relationship with Independent.</p>

<p>Then what is the exact use of insatance of keyword ?
If its used only with "IS-A" relationship then...where are the changes  for the if condition to be false ??</p>
