class inher1
{
public static void main(String...args)
{
eat i = new ursus();
if(i instanceof eat) System.out.println("Instance of eat");
//Line 1
if((omnivora)i instanceof eat) System.out.println("Instance of eat");
//Line 2
if((omnivora)i instanceof omnivora) System.out.println("Instance of omnivora");
if(((omnivora)i).like_honey)System.out.println("Like Honey Obtained");
}
}
interface eat //Interface 1
{
public void eat();
}
interface omnivora //Interface 2
{
final public static boolean like_honey = true;
}
abstract class mammalia implements eat
{
abstract public void makenoise();
public void eat(){System.out.println("Yummy");}
}
class ursus extends mammalia implements omnivora
{
public void makenoise(){System.out.println("Growl");}
}
class felidae extends mammalia
{
public void makenoise(){System.out.println("Roar");}
}
This is my hierarchy
Eat and Omnivora are unrelated interfaces
Mammalia implements Eat Interface
Ursus extends Mammalia implements Omnivora interface
Felidae extends Mammalia
As it can be seen omnivora and eat are unrelated interfaces but yet both Line 1 and Line 2 prints "Instance of eat" and "Instance of omnivora" respectively.
Can someone tell me why ?