5
interface Intf {
}

class A implements Intf {
}

class Test {    
    public static void main(String[] args) {
        Intf obj = new A();
        obj.toString();
    }
}

A friend had shown me this code, I could not explain it to him...

We know that methods defined in 'referred' object can only be run on an instance. As we can see no method is defined by Intf but obj (which refers Intf) is able to call toString() method of Object.class

I consoled him saying that everything is an Object in Java (though we get no autofill option in eclipse IDE against Intf)

aioobe
  • 413,195
  • 112
  • 811
  • 826
anotherNovice1984
  • 397
  • 3
  • 6
  • 13

7 Answers7

11

As we can see no method is defined by Intf

Actually, there is an implicitly declared toString method in Intf.

Every interface (that doesn't explicitly extend another interface) has an implicit method declaration for each public method in Object.

This is explained in detail in the Java Language Specification, § 9.2 Interface Members.

9.2 Interface Members

[...]

  • 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.

[...]

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • And that's not all. This works as well, which I suspect is defined somewhere else in the JLS: `Intf i = ....; Object o = i;` – Bart van Heukelom Jun 03 '11 at 15:48
  • Right. That's covered in [§4.10.2](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.2) which says that `Object` is a supertype of any interface type, which, presumably would also be a valid reference when answering this question. – aioobe Jun 03 '11 at 21:15
2

I'm not sure what your question is, because you're right.

An interface is merely a contract, saying that classes that implement it are required to specify certain methods. In this case, the Intf interface is a no-op since it places no additional requirements on implementors.

Thus your example is functionally identical to

class A {}

Would you be surprised that you can call obj.toString() for that class, when you know that all classes extend java.lang.Object and inherit its methods?

Even in cases where a more involved interface is involved, they simply place more requirements on what a class has to implement. However, all classes ultimately inherit from Object and thus have those methods defined on them.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • 1
    *all classes ultimately inherit from Object and thus have those methods defined on them.* -- All *interfaces* however, does not implicitly extend any "root" interface, as in the case with classes. The static type of `obj` is in this case `Intf`, thus it is not evident from your explanation (and the JLS description for method dispatch) that one should be able to do `obj.toString()`. – aioobe Jun 03 '11 at 13:18
  • Good point; I was addressing this more from the dynamic-type implementation perspective but your answer correctly points out why the compiler can "see" `Intf.toString()`. – Andrzej Doyle Jun 03 '11 at 15:02
1

Every object in Java will inherit from Object, so no matter what objet you create, it will contain a toString().

alexcoco
  • 6,657
  • 6
  • 27
  • 39
RMT
  • 7,040
  • 4
  • 25
  • 37
  • Right, but here the static type of `obj` is `Intf` which does actually *not* inherit from `Object` since it is an interface. – aioobe Jun 03 '11 at 14:20
  • But remember, no matter what Every object inherits from object. its implicit. meaning: class A implements Intf even though you don't see it its really. class A extends Object implements Intf – RMT Jun 03 '11 at 14:23
1

You are implementing an interface that defines no methods and no properties. In effect, this means that you class does not have to define any specific methods/properties (although you are free to define any you wish, there is just no contract to define specific ones).

Every class in Java inherits from java.lang.Object which provides several base methods including toString(). This means that your class has access to all of these methods.

See: http://download.oracle.com/javase/6/docs/api/java/lang/Object.html

Dave
  • 1,303
  • 7
  • 19
1

No Interface does not inherits Object class,but it provide accessibility to all methods of Object class. The members of an interface are:

Those members declared in the interface.

Those members inherited from direct superinterfaces.

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in Object class, .

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.

Now it is clear that all superinterface have abstract member method corresponding to each public instance method declared in Object .
source: http://ohmjavaclasses.blogspot.com/2011/11/is-intreface-inherits-object-clashow.html

Community
  • 1
  • 1
Sheo
  • 1,034
  • 12
  • 24
0

What is the question? Your last sentence is the answer: Everything extends Object in Java

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
  • But interfaces do not formally extend any "root" interface as in the case with classes. And the type of `obj` is in this case `Intf`, so it is clearly a legit question imo. – aioobe Jun 03 '11 at 13:01
0

Chceck this as well. I had ask the same question some time back

Do interfaces inherit from Object class in java

Community
  • 1
  • 1
ponds
  • 2,017
  • 4
  • 16
  • 11