Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have read several articles favoring composition over inheritance and I am convinced that in all those cases composition may be better idea. But does inheritance still has scenarios where it has a clear advantage over composition? Also do I have to implement all properties and methods again in a composed class (as shown in example below)? I am sure I am missing something here...

C#
//May have numerous methods and properties
class BaseEngine
{
	...
	Property101 { get; set; }
	...
	Method101 (parameters...)		
	...
}

//Inheritance
Class SpecialEngine
{
	Override Property101 { get; set; }
	Override Method101 (parameters...)
}

//Composition
Class SpecialEngine
{
	BaseEngine baseEngine = new BaseEngine();

	//Do I need to implement all methods and properties again so that they are callable from special engine?
	Property0 { return baseEngine.Property0 }
	Property1 { return baseEngine.Property1 }
	...
	Property100 { return baseEngine.Property0 }
	Property101 { return special_logic; }

	Method0 { return baseEngine.Method0 }
	Method1 { return baseEngine.Method1 }
	...
	Method100 { return baseEngine.Method100 }
	Method101 { return special_logic; }
}
Posted
Comments
Sergey Alexandrovich Kryukov 30-Aug-11 22:44pm    
What is the purpose of SpecialEngine if it does not anything to BaseEngine? Did you know this "code" will not compile?
--SA
pkkalra1978 31-Aug-11 1:26am    
I thought the approx code will be enough to depict what I am trying to say here. Also my question is general and not specific to any language? I typed that in notepad. I can edit and paste compilable code but I don't think its necessary.
pkkalra1978 31-Aug-11 1:31am    
SpecialEngine overrides few properties and methods from baseEngine (e.g. Property101 and Method101) while retaining most of the base behavior. I use inheritance a lot but was wondering those who advocate composition over inheritance, what they do in these cases.

1 solution

It's good to know that there are many idiots everywhere.

There is one particular sort of extreme idiots who insist on statements in the form "{0} is always better then {1}" in cases when both "compared" approaches are not only very important, but when things are absolutely impossible without either of them. No wonder, being extreme idiots such people suffer from compulsory article writing disorder in cases when reasonable people would prefer to keep silence due to trivial matter of the subject.

That simple consideration explains why you could not see articles with the main topic devoted to the "proof" of such a trivial fact the object-oriented programming is absolutely impossible without inheritance, as well as without composition. Not mentioning "without interfaces" or "without true abstract classes", because programming without them is possible (and actually took place) albeit awkward.

So, we have a situations when there is a mechanism of infiltration of obviously idiotic opinions in publications while there is virtually no publications arguing against those idiotic opinions; really knowledgeable people simply don't take such publications seriously. Arguing against them can hurt one's reputation but cannot change thinking of the stubborn, more exactly, lack of thinking.

Should I even explain why inheritance is important? You mention cases of fields/properties only, and static call dispatch. How about virtual methods, late binding, and, consequently, polymorphism? Those are the cornerstones of OOP and totally based on inheritance (or make no sense without inheritance, as virtual methods). Explanation and application of those mechanisms can be found in any introductory book or an article on OOP. At the same time, there are serious design issues in application of these mechanism to the design of program systems.

Anyone reading any articles should apply some critical thinking and be able to see the essence of things. Not always easy.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900