Comments on: Inheritance is evil, and must be destroyed https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/ Various writings on software development and photography Fri, 10 Aug 2018 08:19:11 +0000 hourly 1 https://wordpress.org/?v=4.9.11 By: charly brown https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-343 Wed, 22 Oct 2014 18:05:14 +0000 http://berniesumption.com/software/#comment-343 dont know if some1 pointed it before, but this topic is a “inheritance versus composition” dilema.

Inheritance isnt evil neither is composition, they just are tools/techniques and you have to use them wisely in your design. (thats why you have to think about your design instead of start writting code and doing iterations in a backstracking (test and error way)

“is-a”: inheritance
“has-a”: composition

lets imagine this 2 classes:
public abstract class Horse extends BaseLifeForm.
public abstract class Truck extends BaseMachine.

this is making an asumption: machines will never be a life form, so you have 2 hierarchies: lifeforms and machines. ¿but what if some day artificial intelligence makes possible that machines became a form of life? UPS!
anyways: horses will never have a “turn on” method…. ¿or not?

at the end, its all about making a design based in some scenario.

disclaimer: english is not my native language so dont blame my mistakes :)

]]>
By: bernie https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-329 Thu, 03 Oct 2013 10:23:45 +0000 http://berniesumption.com/software/#comment-329 I disagree. #FF0080 (DEEP PINK, FUCHSIA) is the greatest of all the colours. Your eyes only bleed if they aren’t awesome enough to handle all that pink!

]]>
By: felis leo https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-328 Wed, 25 Sep 2013 14:52:20 +0000 http://berniesumption.com/software/#comment-328 Google GWT has an hybrid approch :

interface IsXxx {
Xxx asXxx();
}

Yes, you have to copy/paste the Xxx field declaration and this declared getter, but that’s all.
You are free to respect this IsXxx contract by either inheritance or aggregation.
(Even you are no more concerned by method name collision)

Meanwhile, C++ multi-inheritance provides two facilities:
– you don not have to { cast as Xxx / use xxx field / use asXxx getter }
– child classes have access to parent’s protected members

Actually i think the mostly used of inherintance is to simplify a composition.
By exemple, if to make a X you need a common B and custom C
with inheritence: X/B are base, C extends X/B
with agregation: X is interface, B and C classes, C’ assemble B and C to become a X
and you need extra interfaces to define communication between B and C, and the rest of the world…

Finally I agree: inheritence is bad, but a shortcut.

]]>
By: anonymous https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-327 Mon, 01 Jul 2013 00:59:53 +0000 http://berniesumption.com/software/#comment-327 Pink at the top of the page template is eye bleeding (#ff0080). The bottom color of the gradient (#b3002d) goes nice with the textured grey.

]]>
By: Nick https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-326 Sun, 02 Jun 2013 06:38:40 +0000 http://berniesumption.com/software/#comment-326 C++ multiple inheritance works great! The classic diamond problem is solved by the compiler mentioning the ambiguity of your code. I have bent over backwards trying to find a work around to not having multiple inheritance in C# (or same thing with Java, AS3). The best I can do is create an interface of the second class I want to derive from and, eeeeeeeeewwwwww, copy and paste the implementations of its methods. Such a shame. I blame Java for this. Correct me if im wrong but they are the bastards that started the whole interface thing.

]]>
By: NoQuarter https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-325 Thu, 23 May 2013 22:44:26 +0000 http://berniesumption.com/software/#comment-325 ———————————————————————-
To: thpigdog
———————————————————————-
Now say we have a rule “all dogs chase cats”.
dog.See(cat) implies dog.Chase(cat)

This could be written as a method,

void Dog.See(Cat cat)
{
Chase(cat);
}

Now suppose we have a class CatLovingDog that inherits from Dog.

void CatLovingDog.See(Cat cat)
{
Greet(cat);
}
———————————————————————-

No, rather than creating “some other kind of dog”, (the same fundamental inheritance problem discussed in the article), instead the thing to do is to modify the existing dog definition by adding a “CatAffinity” property.

void Dog.See(Cat cat)
{
if (this.CatAffinity > 0)
Greet(cat);
else
Chase(cat);
}

Chase or greet is not an “is-a”, nor is it a “has-a”. Instead it’s an attribute of a specific instance.

]]>
By: Peter https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-324 Mon, 07 Jan 2013 09:55:08 +0000 http://berniesumption.com/software/#comment-324 I’m relieved that I’m not alone with my view of disliking inheritance.
And yes my main problem with it is that it’s against encapsulation as sy already wrote (I just couldn’t name the feeling).

]]>
By: DevlinB https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-323 Sat, 22 Dec 2012 02:47:22 +0000 http://berniesumption.com/software/#comment-323 Sounds like the problem is a lack of Interfaces. DarkJediPowers should be an interface that anyone can implement.

Your Ball example actually works equally well with this!

Interfaces when all implementations are best represented using multiple inheritance of course. Then again in my experience it is rare that a DarkHippo is going to crush villagers in the same way a DarkJedi will. :)

]]>
By: Jimshell https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-322 Mon, 03 Dec 2012 08:41:40 +0000 http://berniesumption.com/software/#comment-322 Hi Bernie,
I feel a bit like a spammer right now ^^
Speak so much that I forgot to mention something interesting (and important) as of patterns and inheritance: the Deocorator pattern.
Basically, it is sort of a substitute for sub-classing, but it should be used in some specific occasions only (as always with patterns).
Say: if the (modified) behavior has to to be added/removed at run time, or if you need to combine the functionalities from multiple decorators (where multiple inheritance is not possible, like in PHP, or not suitable nor wishable)

]]>
By: Jimshell https://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/#comment-321 Mon, 03 Dec 2012 03:49:42 +0000 http://berniesumption.com/software/#comment-321 Sorry for all the typos in my answer (made a lot)
Hope it’s still clear enough ^^

]]>