Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Inheritance really needed?

I must confess I'm somewhat of an OOP skeptic. Bad pedagogical and laboral experiences with object orientation didn't help. So I converted into a fervent believer in Visual Basic (the classic one!).

Then one day I found out C++ had changed and now had the STL and templates. I really liked that! Made the language useful. Then another day MS decided to apply facial surgery to VB, and I really hated the end result for the gratuitous changes (using "end while" instead of "wend" will make me into a better developer? Why not drop "next" for "end for", too? Why force the getter alongside the setter? Etc.) plus so much Java features which I found useless (inheritance, for instance, and the concept of a hierarchical framework).

And now, several years afterwards, I find myself asking this philosophical question: Is inheritance really needed?

The gang-of-four say we should favor object composition over inheritance. And after thinking of it, I cannot find something you can do with inheritance you cannot do with object aggregation plus interfaces. So I'm wondering, why do we even have it in the first place?

Any ideas? I'd love to see an example of where inheritance would be definitely needed, or where using inheritance instead of composition+interfaces can lead to a simpler and easier to modify design. In former jobs I've found if you need to change the base class, you need to modify also almost all the derived classes for they depended on the behaviour of parent. And if you make the base class' methods virtual... then not much code sharing takes place :(

Else, when I finally create my own programming language (a long unfulfilled desire I've found most developers share), I'd see no point in adding inheritance to it...

like image 556
Joe Pineda Avatar asked Nov 10 '08 16:11

Joe Pineda


People also ask

Is inheritance needed?

Inheritance enables code reusability and saves time. Inheritance is used to declare characteristics of classes inheriting it,without giving its implementation.It is one of the most important concept of OOPS. Inheritance is an object-oriented property of java. Inheritance is very essential for expandability.

What is the point of using inheritance?

Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces.

Is there any alternative for inheritance?

Delegation can be an alternative to inheritance. Delegation means that you use an object of another class as an instance variable, and forward messages to the instance.

Are there any reasons why a programmer might not want to use inheritance?

To summarize the above, inheritance can cause a few problems: We couple an interface (a type) with a specific implementation. We have difficulty evolving our code in the future, thanks to unrestricted open recursion. Part of the reason we like objects is to model state, but state and variance don't play well together.


1 Answers

Really really short answer: No. Inheritance is not needed because only byte code is truly needed. But obviously, byte code or assemble is not a practically way to write your program. OOP is not the only paradigm for programming. But, I digress.

I went to college for computer science in the early 2000s when inheritance (is a), compositions (has a), and interfaces (does a) were taught on an equal footing. Because of this, I use very little inheritance because it is often suited better by composition. This was stressed because many of the professors had seen bad code (along with what you have described) because of abuse of inheritance.

Regardless of creating a language with or without inheritances, can you create a programming language which prevents bad habits and bad design decisions?

like image 74
Seamus Avatar answered Sep 22 '22 19:09

Seamus