Varun
Varun

Reputation: 3311

Casting on objects implementing unrelated interfaces Java

    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 ?

Upvotes: 4

Views: 447

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074495

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 ?

Because the object you're testing is an ursus, which implements omnivora and also implements eat (by inheriting from mammalia).

One of the key things about interfaces is that a class can implement multiple, unrelated interfaces, and its instances are considered "instances of" each of those interfaces.

A concrete example would be FileReader, which extends Reader but also implements Readable, Closeable, and AutoCloseable. Those last two are unrelated to reading, per se, but the class's instances are instances of all of those interfaces.

Upvotes: 4

Mario Rossi
Mario Rossi

Reputation: 7799

Operator instanceof doesn't check against the static type of an object but against its dynamic type. (omnivora)i instanceof eat is exactly the same as i instanceof eat.

No matter how much you cast it, the dynamic type of i is ursus, which is a subclass of eat and omnivora. The result can't be different to what you are obtaining.

Please respect Java's naming conventions. Your program is extremely difficult to read.

Upvotes: 1

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

Mammalia implements both eat and omnivora, which means that any of the subclasses of Mammalia is a subtype of both of these interfaces. So any ursus object is both an eat and an omnivora.

Upvotes: 1

Related Questions