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?

3
  • 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. Commented Jul 9, 2012 at 2: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? Commented Jul 9, 2012 at 2:52
  • Since everything is Object (except for primitive type), the type of reference doesn't really matter. I guess that's how it works. Commented Jul 9, 2012 at 3:00

3 Answers 3

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.

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

3 Comments

+1 for linking to the JLS. :)
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. :)
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?
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.

2 Comments

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?
Because interfaces are classes, and all classes in Java inherit from java.lang.Object. So a Workable is an Object.
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

3 Comments

That's hashCode(), not getHashCode(). It's GetHashCode() in C#, though.
I have a problem with mixing languages
It happens to me a lot, too. Just noting it. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.