• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance is a waste  RSS feed

 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about IS-A relationship but I think the use of inheritance is just polymorphism when we override some methods.
Code reuse doesn't seem to be a good use of inheritance as we can do that using objects.

And what's "implementing" interfaces, isn't it doing what inheritance should do?
They provide abstraction, polymorphism, surety (it's compiler friendly contract behavior).

Isn't inheritance is just a waste ? ( I am not mature to say that )

Or for just giving an OO view to your application inheritance should be chosen? For example : Tiger is a Cat but not Catable

Please reply..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementing an interface has the same meaning as extending a class, with regard to inheritance: the same is a relationship holds. For example, a class that implements interface Runnable is a Runnable.

It is indeed not a good idea to use inheritance just for code reuse (don't extend a class just because it would be nice to be able to access a method that's defined in the class).

Sagar Dabas wrote:Isn't it inheritance is just a waste?

No, it's very useful. And implementing interfaces is also inheritance.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Sagar Dabas wrote:Isn't it inheritance is just a waste?

No, it's very useful. And implementing interfaces is also inheritance.


How ? Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?

Thanks, for reply

Seetharaman Venkatasamy wrote:
http://www.coderanch.com/t/564467/java/java/why-there-no-multiple-inheritance
see the Jeff reply!


Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Dabas wrote:I know about IS-A relationship but I think the use of inheritance is just polymorphism when we override some methods.


That's the main point of it, yes.

Code reuse doesn't seem to be a good use of inheritance as we can do that using objects.


It's a nice side benefit. And as for "do that using objects", it sounds like you're talking about "prefer composition over inheritance."(⇐click) If so, you're on the right track.

And what's "implementing" interfaces, isn't it doing what inheritance should do?


It's a form of inheritance. It gives us a way to define a pure abstract type.

Isn't inheritance is just a waste ? ( I am not mature to say that )


You mean extending classes? It has value. While inheritance for code sharing tends to be abused and overused, there are still cases where it's useful to inherit some implementation.

Or for just giving an OO view to your application inheritance should be chosen? For example : Tiger is a Cat but not Catable


No idea what you're trying to say here or what point you're trying to make.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Dabas wrote:
Jesper de Jong wrote:
Sagar Dabas wrote:Isn't it inheritance is just a waste?

No, it's very useful. And implementing interfaces is also inheritance.


How ?


How what? How is it useful? Or how is implementing interfaces also inheritance?

If the former, I explained in my previous reply.

If the latter, you hit it on the head in your first sentence of this thread, though perhaps you didn't realize it: Extending a class, extending an interface, and implementing an interface are all type specialization via an IS-A relationship.

Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.



Thanks you so much. I got it now.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sagar Dabas wrote:Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.


Wait, are you saying that a class hierarchy has no value in itself? Cause if you are, I vehemently disagree.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
Jeff Verdegan wrote:
Sagar Dabas wrote:Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.


Wait, are you saying that a class hierarchy has no value in itself? Cause if you are, I vehemently disagree.


No, no, not at all. I'm simply saying that the only thing that extending a class gives you that you don't get from implementing an interface is code reuse. Both give you type specialization. The difference is that a class can have implementation, while an interface can't. So the benefit that, say, ArrayList and LinkedList get from extending AbstractWhateverList as opposed to simply implementing List is that a lot of the common grunt-work is already done for them.

 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Dennis Deems wrote:
Jeff Verdegan wrote:
Sagar Dabas wrote:Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.


Wait, are you saying that a class hierarchy has no value in itself? Cause if you are, I vehemently disagree.


No, no, not at all. I'm simply saying that the only thing that extending a class gives you that you don't get from implementing an interface is code reuse.


But I still disagree. Extending a class gives you a place in a class hierarchy. Implementing an interface does not. Implementing Quackable doesn't make you a duck. Implementing every interface implemented by Duck doesn't make you a duck. Extending Duck does this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
Jeff Verdegan wrote:
Dennis Deems wrote:
Jeff Verdegan wrote:
Sagar Dabas wrote:Ok then, what extending can do which implementing cannot do? Is it just the CODE REUSE ?


Yup. That's it. And that is valuable, when used properly.


Wait, are you saying that a class hierarchy has no value in itself? Cause if you are, I vehemently disagree.


No, no, not at all. I'm simply saying that the only thing that extending a class gives you that you don't get from implementing an interface is code reuse.


But I still disagree. Extending a class gives you a place in a class hierarchy.


Just as implementing an interface gives you a place in a type hierarchy.

Implementing an interface does not.


You seem to think that a class hierarchy is something special or better than a type hierarchy, but the only differences is that a class is a particular kind of type that can carry implementation.

Implementing Quackable doesn't make you a duck.


So? We have an IS-A relationship with the class we extend, with every interface we implement, and with all their supertypes. And we don't have that relationship with any other type. So whether we extend a class or implement an interface, we inherit some types and not others. The only difference is that extending the class might give us implementation.

And if Duck is an interface, then implementing Duck does make us a Duck.

Implementing every interface implemented by Duck doesn't make you a duck. Extending Duck does this.


As does implementing Duck if Duck is an interface.

And if Duck is a class, then extending Duck doesn't make you a Cloneable, Serializable, or Comparable if Duck doesn't implement those interfaces, but implementing them does. We are what we inherit from, and we are not those types we do not inherit from. This is the same for both classes and interfaces.

Both "extends" and "implements" mean basically the same thing--"inherits from" or "IS-A".

A class and an interface are both types. Implementing an interface, extending a class, or extending an interface all give us a place in a type hierarchy. The only additional thing that extending a class gives us is (maybe) implementation.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah but it begs the question, why would you want to extend Duck?

[edit]

My reply was in response to Dennis' last post.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yeah but it begs the question, why would you want to extend Duck?


Well, if you extend a Duck's neck, it kind of looks like a Goose.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!