Hi guys,
I need explanation for the following marked line, which is in test2 class:
public class InstanceofClass {
public static void main(
String[] args) {
test2 t = new test2();
if(t instanceof Object)
System.out.println("t instance of Object");
if(t instanceof test1)
System.out.println("t instanceof test1");
if(t instanceof testInterface)
System.out.println("t instanceof testIterface");
System.exit(0);
}
}
class test2 extends test1{
public test2(){
if(this instanceof test1)
System.out.println("test2 instanceof test1");
}
}
class test1 implements testInterface{
public test1(){
if(this instanceof test2)//need explanation ???
System.out.println("test1 instanseof test2");
}
public void output(){
System.out.println("interface method");
}
}
interface testInterface{
void output();
}
-----------------------------------------------------------------------
When I compile and run the program i get:
test1 instanseof test2
test2 instanceof test1
t instance of Object
t instanceof test1
t instanceof testIterface
Why test1 instanceof its subclass?