loading...

What's so bad about inheritance in comparison to composition?

pandaquests profile image pandaquests ・1 min read

Discussion

pic
Editor guide
Collapse
vonheikemen profile image
Heiker

The thing about inheritance is that you have to be careful when you design the hierarchy of your classes. It might solve an immediate problem that you have but later down the road they can become an obstacle to implement a new feature. I've heard people say that you should only use inheritance when you are absolutely sure you only need a Parent and Child class, that is a hierarchy with just one level.

When you use composition techniques it should be easier to just add new code without worrying about old code.

Collapse
frau2015 profile image
skal

Inheritance in a class based system, everything is inherited from child to parent. If you have one branch of classes you can't brach to another one, which is bad about inheritance. You can't make new connections to other hierarchies, so if you decide that one class should also inherent from another class from a different hierarchy than you'll have to make a new design will includes that other class in your classes hierarchy. This in flexibility gets annoying and difficult to mange on large systems maybe its okay if you writing small projects

Collapse
blindfish3 profile image
Ben Calder

Imagine a big stack of cubes and having to swap one out at the bottom of the pile. That's inheritance.
Now imagine all those cubes side by side and making a change to one of them.

It's probably an oversimplification; but unless very well structured inheritance sets you up for a fall...