Composition and Inheritance are two words in Object-Oriented languages used on a daily basis. When you are trying to explain some problem using design patterns, it’s very unlikely to escape these two magic words. Well, the question I always ask or want to ask is, do you know what they mean and when and where to use them in actual contexts? I often see developers asking the same question again and again. Let me try my best to explain it.
Inheritance describes a superclass-childclass relationship where a derived class or child class has all the features of the super class or parent class.
Inheritance
It defines a relationship of type “is-a”.
Examples
- A car is a vehicle
- TextEditor is an editor
- HtmlEditor is an editor
Inheritance
Let’s say a class B (child class) inherits from class A (base class or super class). Therefore, class B has access to all methods and fields of type public or protected from Class A.
Class A
- public class A{
- public bool MethodA()
- {
- return true;
- }
- }
- class B: A
- {
- public bool MethodB()
- {
- return base.MethodA();
- }
- }
Composition
It defines a relationship of type “has-a”.
Example
- A car has an engine.
- A house has a kitchen.
- A motherboard has a processor
- A text editor has a button and text area
Class A
- class A{
- public bool MethodA()
- {
- return true;
- }
- }
- //composition way
- class B
- {
- A a= new A();
- public bool MethodB()
- {
- return a.MethodA();
- }
- }
Now let’s say in our client we invoke a method from class B that in turn invokes MethodA() from class A.
- main(string [] args)
- {
- B b= new B();
- Console.writeline(b.MethodB()); // in this case the writeline function would always print Boolean value as true.
- }
Let’s make the difference:
Now let’s say instead of returning true from MethodA() from base class (Class A), we modify its behavior. Now it returns a string instead of a bool.
Changed Code from Class A
- Class A
- {
- public string MethodA()
- {
- return “Mario”;
- }
- }
Now understand it from the Composition point of view as in the following:
- Class B
- {
- A a= new A(); // composition
- Public bool MethodB()
- {
- return a.MethodA()==”Mario”?true:false;
- }
My argument is not about choosing composition over inheritance. The decision should be based on the proper relationship hierarchy among classes. My point is, to favor Composition over Inheritance is code reuse without extending a class. Which means one class can reuse multiple classes but with Inheritance you are only limited to reusing code from one class.

Bhuvanesh MohankumarPosted May 10, 2016, 2:28 AM
Good one..
Sonu ChaudharyPosted Mar 16, 2016, 3:23 AM
good
Ratnesh SinghPosted Sep 2, 2015, 3:51 AM
nice
Yashwanth MuthineniPosted Aug 22, 2015, 7:25 AM
Nice Share
Santhakumar MunuswamyPosted Aug 22, 2015, 7:15 AM
Good Article. Thanks for sharing
Nilesh JadavPosted Aug 21, 2015, 9:51 AM
Thanks for sharing
Gowtham KPosted Aug 21, 2015, 3:56 AM
Good one
Rajeesh MenothPosted Aug 21, 2015, 12:18 AM
Good Share..
Vaikesh K PPosted Aug 21, 2015, 12:14 AM
Good
RakeshPosted Aug 21, 2015, 12:10 AM
Good share sir
Gopi ChandPosted Aug 20, 2015, 11:39 PM
Nice work
Karthikeyan KPosted Aug 20, 2015, 11:11 PM
Good one sir...
Mohammed IbrahimPosted Aug 20, 2015, 11:02 PM
nice
Pankaj Kumar ChoudharyPosted Aug 20, 2015, 10:54 PM
Nice Explain Sir...........