• Post Reply Bookmark Topic Watch Topic
  • New Topic

Superclass?  RSS feed

 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class implements an interface, does that make the interface its superclass?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:If a class implements an interface, does that make the interface its superclass?

I think the correct term would be supertype; but the two things are closely related, and both indicate an "IS-A" relationship.

In terms of mechanics, the differences are that you can implement more than one interface, but only extend one superclass. Also: you can't use the super keyword to refer to an interface ([Edit] actually, that may not be strictly true - I've never checked - but there's no real reason to, since all members of an interface are public, and interfaces don't contain any implementation).

But in terms of use, the two things are almost synonymous. You can also create anonymous classes directly from interfaces.

HIH

Winston
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ryan McClain wrote:If a class implements an interface, does that make the interface its superclass?

I think the correct term would be supertype; but the two things are closely related, and both indicate an "IS-A" relationship.

In terms of mechanics, the differences are that you can implement more than one interface, but only extend one superclass. Also: you can't use the super keyword to refer to an interface.

But in terms of use, the two things are almost synonymous. You can also create anonymous classes directly from interfaces.

HIH

Winston


Thank you. I was asking this question because my IDE at one point showed me the warning message in the likes of: "Your method does not override the method of its superclass".
Someone told me this is because you are using @Override with a method that does not exist, hence the superclass of that method does not exist.
This happened when I implemented the Comparable interface and I wanted to impplement the compareTo(Object o) method, using my own custom compareTo(Ball ball) method. Later, I found out I needed to implement Comparable<Ball> to fix this problem/warning.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:This happened when I implemented the Comparable interface and I wanted to impplement the compareTo(Object o) method, using my own custom compareTo(Ball ball) method. Later, I found out I needed to implement Comparable<Ball> to fix this problem/warning.

Right. Which is why it's a really good idea to use @Override when you're overriding a method - especially with equals().

Almost everybody at some point (me included), forgets that the signature of equals() is specifically:

public boolean equals(Object other) { ...

and if you put the @Override in, you'll get an error if you don't write it precisely that way; otherwise, the compiler will happily compile your class.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through the Java Language Specification, you find that seems to be called a superinterface. The error message says “superclass” regardless. It is simply the wording of the error message.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!