Extends versus Implements
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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! 
thanks! 
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).
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).
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Plenty of resources around for this. For example:
http://stackoverflow.com/questions/10839131/implement-vs-extends-when-to-use-whats-the-difference
http://stackoverflow.com/questions/10839131/implement-vs-extends-when-to-use-whats-the-difference
Tim Driven Development | Test until the fear goes away
posted 12 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
I sometimes wish the language had just picked a single keyword to use for these situations.
posted 12 years ago
<nitpick>
I don't want to confuse anyone, but a subclass can also implement an interface.
</nitpick>
Winston
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 12 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I think Winston was referring to the combination case, eg where class B extends class A and implements interface I
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Tony Docherty wrote:I think Winston was referring to the combination case, eg where class B extends class A and implements interface I
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 12 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 |












