• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extends versus Implements  RSS feed

 
Als Alajas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys, can you please explain it to me the difference between extends versus implements in java? drop down an example for each keyword please. im kinda new in here so if my question is redundant, then sorry. thanks!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Als. Welcome to the Ranch!

Do you know the difference between a class and an interface? A class can extend another class (to inherit from it) and it can implement an interface.

(And as a slight complication, an interface can extend another interface, but don't worry about that until you've got the first bit straight).
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plenty of resources around for this. For example:

http://stackoverflow.com/questions/10839131/implement-vs-extends-when-to-use-whats-the-difference
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They really mean basically the same thing--"inherits from"--and in any given situation, only one of them can be used, and if you use the wrong one, the compiler will tell you. So there's not lot of "meaning" or rules that you really need to learn.

I sometimes wish the language had just picked a single keyword to use for these situations.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:They really mean basically the same thing--"inherits from"--and in any given situation, only one of them can be used

<nitpick>
I don't want to confuse anyone, but a subclass can also implement an interface.
</nitpick>

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeff Verdegan wrote:They really mean basically the same thing--"inherits from"--and in any given situation, only one of them can be used

<nitpick>
I don't want to confuse anyone, but a subclass can also implement an interface.
</nitpick>




Yes, I know, and that's covered by what I said:

class inherits-from interface : implements
class inherits-from class : extends
interface inherits-from interface : extends
interface inherits-from class : not allowed
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Winston was referring to the combination case, eg where class B extends class A and implements interface I
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless, Jeff's point is valid - for each place where you use either "extends" or "implements" in the code, only one of those two choices is valid, based on whether the supertype and subtype are classes or interfaces. And if you get that wrong, the compiler will tell you. Which means that if they had just defined one such term, the compiler would be able to infer what it needed to know, from the context where it's used. No added information is conveyed by having an extra term here; it just gives us more opportunities to create compilation errors by accidentally using the wrong term.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, for the combined case

where B is a class and C is an interface, we know that the word between A and B can only be "extends", because A and B are classes. And the word before C can only be "implements", because A is a class and C is an interface. That's what Jeff meant by only one choice being possible in each case.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I think Winston was referring to the combination case, eg where class B extends class A and implements interface I




 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use extends inside <> as part of an actual type parameter. The one justification for having two keywords I can see is to distinguish between single inheritance and implementing multiple interfaces.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can use extends inside <> as part of an actual type parameter. The one justification for having two keywords I can see is to distinguish between single inheritance and implementing multiple interfaces.


Yeah, the separate keywords do make for a nice visual cue. Even without them, there would be no ambiguity in the language. It would mean though that we wouldn't know just from the "inherits-from" clause whether each of the named types was an interface or a class, just like we can't tell in the <? extends ...> case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!