• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Extends versus Implements

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 6223
503
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 5252
85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 5252
85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Marshal
Posts: 81621
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic