4
    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 ?

2
  • 2
    Please remember that Interface names and Class names should start in an upper-case; not a lower-case. Commented Aug 25, 2013 at 9:16
  • indirect implementation of both interface because " Ursus extends Mammalia implements Omnivora interface" (mammalia implements eat) Commented Aug 25, 2013 at 9:17

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.