• Post Reply Bookmark Topic Watch Topic
  • New Topic

relation between Object class and interfaces?  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that every interface contains all non final method of Object class.As these methods have implementations in Object class and interface don't support implementation of method inside the interface(not until jdk 8).So how method with implementation loses its body for coming into interface?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:I found that every interface contains all non final method of Object class.

How so?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does that support your assertion?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can access tostring() method which i won't be able to do if interface would not have been having all non final method of Object class without implementation
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All classes implicit inherit from Object including Base.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but parent reference variable is of type Parent(interface) and it won't be able to access tostring() if interface Parent don't have it
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:but parent reference variable is of type Parent(interface) and it won't be able to access tostring() if interface Parent don't have it


Debatable. I guess you can conclude that interfaces subtype from the Object class. Or another way to interpret it is ... the special case of interface with no direct superinterface is handled by the Java Language Specification.

http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.10.2

Which interpretation is correct? I am not sure if it matters.

Henry
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no this occurs ,even if an interface extends any interface.There is no difference if an interface has no direct superinterface or it does have
ya! i think we can consider it to be subtype
but in Object class they have proper implementation.How they loose their implementation in interface (I am talking about jdk 7)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:no this occurs ,even if an interface extends any interface.There is no difference if an interface has no direct superinterface or it does have
ya!
i think we can consider it to be subtype but in Object class they have proper implementation.


I think you missed the point. When there is no direct super interface, the JLS says that the interface must support the type of Object. If there is a direct super interface, then it also supports that superinterface, etc. etc. etc., until it reaches an interface without a direct superinterface, and hence, it must support the type of Object.

In other words, any interface, will have either have no direct superinterface, or have a superinterface with no direct superinterface, and hence, must support the Object type.

Sachin Tripathi wrote:How they loose their implementation in interface (I am talking about jdk 7)


Interfaces don't have an implementation, hence, it didn't lose anything. And as for why it supports the type of Object, that is already explained in the JLS.

Henry

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Henry it was very helpful..understood it
Henry Wong wrote:Interfaces don't have an implementation, hence, it didn't lose anything. And as for why it supports the type of Object, that is already explained in the JLS.

I wanted to ask that the methods will be having their implementation how they loose their body for coming in interface as it donot allow methods with body



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!