1

Possible Duplicate:
Do interfaces inherit from Object class in java

package inheritance;
class A{
   public String display(){
       return "This is A!";
   }
}

interface Workable{
   public String work();
}

class B extends A implements Workable{
   public String work(){
      return "B is working!";
   }
}

public class TestInterfaceObject{
   public static void main(String... args){
      B obj=new B();
      Workable w=obj;
      //System.out.println(w.work());
      //invoking work method on Workable type reference

      System.out.println(w.display());
      //invoking display method on Workable type reference

      //System.out.println(w.hashCode());
      // invoking Object's hashCode method on Workable type reference
    }
}

As we know that methods which can be invoked depend upon the type of the reference variable on which we are going to invoke. Here, in the code, work() method was invoked on "w" reference (which is Workable type) so method invoking will compile successfully. Then, display() method is invoked on "w" which yields a compilation error which says display method was not found, quite obvious as Workable doesn't know about it. Then we try to invoke the Object class's method i.e. hashCode() which yields a successful compilation and execution. How is it possible? Any logical explanation?

Community
  • 1
  • 1
PrashantGupta
  • 83
  • 2
  • 10
  • 2
    All objects are `Object` in Java. That's probably why it can invoke methods of `Object` regardless. I think someone will quote from the standard to answer the question properly. – nhahtdh Jul 09 '12 at 02:36
  • What methods are going to be invoked on an object depends upon the type of the reference which is referring to that object. Here, Workable type reference is referring to the object and How does Workable type reference know about the Object class's methods? – PrashantGupta Jul 09 '12 at 02:52
  • Since everything is `Object` (except for primitive type), the type of reference doesn't really matter. I guess that's how it works. – nhahtdh Jul 09 '12 at 03:00

3 Answers3

4

The intuitive answer is that regardless of what interface you refer to, the object implementing the interface must be a subclass of Object.

Section 9.2 of the JLS specifically defines this behaviour: http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.2

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

i.e. all interfaces are assumed to contain method signatures corresponding to methods in the Object class.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • 2
    +1 for linking to the JLS. :) – David Conrad Jul 09 '12 at 03:39
  • Thank you. I would like to add more to this from JLS. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object. It follows that is a compile-time error if the interface declares a method with a signature that is override-equivalent (§8.4.2) to a public method of Object, but has a different return type or incompatible throws clause. This explains everything now. :) – PrashantGupta Jul 09 '12 at 03:46
  • Does this answer also holds good for `abstract class{}`, I mean Does `abstract class` implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object or Does `abstract class` inherits the implementations of `class Object`? – overexchange Dec 09 '14 at 16:54
2

I think what's happening here is that even though w is known only to be Workable, all objects must derive from Object, so no matter what class w eventually is, it must have the Object methods.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • What methods are going to be invoked on an object depends upon the type of the reference which is referring to that object. Here, Workable type reference is referring to the object and How does Workable type reference know about the Object class's methods? – PrashantGupta Jul 09 '12 at 02:51
  • Because interfaces are classes, and all classes in Java inherit from java.lang.Object. So a Workable ***is an*** Object. – David Conrad Jul 09 '12 at 03:38
1

The reason w.display() doesnt work is as you have save the reference as your interface type. The compiler only sees the methods exposed by the interface. If you were to call ((B)w).display() this would work. You are able to call hashCode() as the compiler is smart enough to know that interfaces are inherited by Objects and all object's superclass is Object

secretformula
  • 6,414
  • 3
  • 33
  • 56