• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why interface inherit all the non final methods from the Object class ?  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone help me to understand this that why interfaces inherit prototype of all the non final methods of the object class in itself?

Object class is parent class of all the class and Interface is not the class.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces do not (as far as I can remember) inherit anything from Object. You can confirm or refute that by looking in the Java Language specification.
The classes instantiated from that interface, however, do inherit all non‑private members of Object.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you thinking about something like this?

In that case the Comparable interface doesn't inherit hashCode(). However, the compiler knows that any object that implements Comparable will also have inherited the hashCode() method, and so it's guaranteed to be safe.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give you small example:



You see above that i make reference variable of m of interface type and assigned reference id of object of the temp class. Now i run toString method. with the reference variable of the parent always override method of the child call or parent method call.
If prototype of toString() is not there in the parent then it should give error but i am afraid it is not. It prints "Hello". which proves me that somehow "toString" method is present in it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew explained why this occurs. I suggest you re-read his post and substitute toString() for hashcode().

BTW Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code and the easier it is for people to read your code the more likely you are to get an answer.
I've added them for you this time.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already had it explained. when you create a class which implements the interface, e.g. public class Foo implements Comparable, or even as an anonymous class, you are no longer dealing with an interface. You have a class. And as you already know, all classes implicitly extend Object.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Matthew explained why this occurs.

But his explanation is wrong.
Look at the link to the JLS that Campbell provided. The third bullet point of 9.2 gives the proper reason why this works.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote: . . . Look at the link to the JLS that Campbell provided. The third bullet point of 9.2 gives the proper reason why this works.
Yes, you are right. It neither confirmed nor refuted what I thought, but it does explain it.
When you instantiate a class, that will have concrete methods inherited from Object which act as the implementation of those methods.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:But his explanation is wrong...

Actually, I'm not so sure it is. 9.2 explains the effect, not the mechanism; and it seems perfectly reasonable to me that the compiler would understand that anything that is instantiated must be an Object.

I'm also not totally happy with the wording. Wouldn't the declaration of an abstract method require any subclass to re-implement it? I have to admit, I'm not at all sure about this, since I've never tried it. Maybe I'll give it a go later on.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Stuart A. Burkett wrote:But his explanation is wrong...

Actually, I'm not so sure it is. 9.2 explains the effect, not the mechanism

I disagree
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

So every interface, directly or indirectly via a parent interface, has a toString (for example) method declared in it. Therefore when the compiler sees something like

it will just check (as it does with any other method) that the MyInterface method has a toString method, find that it does and is quite happy. No need for the compiler to treat the Object class methods any differently to methods declared in other interfaces. No need for it to 'know' that the Object class has a toString method.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pardon me for asking again but i am beginner to this technology.

As per JLS 9.2 it is mentioned that it is implementing the same but how and why does it is happening still not cleared to me. I also tried to decompile the interface .class file and trying to see if it is inheriting anything but not showing anything significant. Though when we see class it showing inheriting Object class.





 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That javap output suggests an interface does not extend Object. It only shows explicitly declared methods, however.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:As per JLS 9.2 it is mentioned that it is implementing the same but how and why does it is happening still not cleared to me.

There is no mention of implementation of anything in section 9.2 of the JLS.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It only shows explicitly declared methods, however.

And that was my point. The word "implicit" suggests an effect to me, not necessarily precisely what is occurring. And the minute you use the 'something'-dot notation, surely the compiler can assume that 'something' is either a non-null object or a class.

I honestly don't know, but it's interesting stuff...albeit perhaps of the "does it really matter?" kind.

Winston
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actually i was thinking why Technology creator did this that's why curious to know why it is happening but in reality it is not matter much.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
Actually i was thinking why Technology creator did this that's why curious to know why it is happening but in reality it is not matter much.


I don't know about the exact why. But following is my logic.
<Guessing>
When we declare a reference variable as follows..



we can invoke only those methods on obj that are there ( either declared or inherited ) in SomeSuperClass. If SomeSubclass overrides/defines any of these methods, run time polymorphism will ensure the subclass methods are invoked. Every class inherits the public methods of the Object class. The designers probably didn't want Java to have the interface references be treated any differently; for us to invoke a method on a reference variable, the method has to be present in the reference type. Hence the object class public methods are implicitly declared as abstract methods in the interfaces ( in the interface that's the parent of all interfaces in a hierarchy of interfaces ).

Note : we can't invoke a sub interface method in a class that implements the sub interface via a super interface reference. Same applies to the classes.
</Guessing>
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few points:
1. JLS Section 9.4 - all interface methods are implicitly public and abstract. The JLS 9.3 explicitly states "implicitly declares public abstract" for completeness.
2. The reason public methods from Object are implicitly included in an interface declaration probably has to do with dynamic binding, so whoever said "the compiler knows" is (probably) right and whoever referenced the JLS is also right.
3. This is a bit over the head for "Beginner" - move to General maybe?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few points:
1. JLS Section 9.4 - all interface methods are implicitly public and abstract. The JLS 9.3 explicitly states "implicitly declares public abstract" for completeness.
2. The reason public methods from Object are implicitly included in an interface declaration probably has to do with dynamic binding, so whoever said "the compiler knows" is (probably) right and whoever referenced the JLS is also right.
3. This is a bit over the head for "Beginner" - move to General maybe?


Yup thanks..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!