• Post Reply Bookmark Topic Watch Topic
  • New Topic

is interface can extends any class  RSS feed

 
S.Reddisekhar Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface is can't extend any class,
but how it is extending Object class features internally

Code Snippet::

public interface Interface {
public void m();
}


public class ImplementedClass implements Interface{

public void m() {
// TODO Auto-generated method stub

}
public static void main(String[] args) {
Interface i = new ImplementedClass();
i.m();
i.toString();
i.hashCode();
}

}
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces don't extend Object. However, the compiler knows that any concrete instance of that interface must be of a class that does extend Object. Therefore it's safe to allow the methods of Object to be called.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Interfaces don't extend Object

But they do implicitly contain a declaration of all public methods in the object class. JLS - third bullet point
 
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 they do implicitly contain a declaration of all public methods in the object class. JLS - third bullet point

Hmmm. Seems like an odd way to do things. That tends to suggest that any class that implements a top-level interface has to provide an implementation for all Object methods, which I'm pretty certain is not the case. It certainly would be the case if it was an abstract class though.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Hmmm. Seems like an odd way to do things. That tends to suggest that any class that implements a top-level interface has to provide an implementation for all Object methods

But they already do that, simply by implicitly extending Object.

In practice there's very little difference. I suspect the only meaningful difference between the two interpretations would be what you'd get if you used reflection to list the methods in an interface.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:But they already do that, simply by implicitly extending Object.

Ah, but the same could also be said of any class that extends an abstract one; but if that abstract class defines an abstract method with the same signature as one of Object's, then you must implement it. That's why the setup seems odd to me.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!