Composition and Inheritance in Object Oriented Programming

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

  1. public class A{  
  2.   
  3.    public bool MethodA()  
  4.    {  
  5.       return true;  
  6.    }  
  7. }  
Class B
  1. class B: A  
  2. {  
  3.   
  4.    public bool MethodB()  
  5.    {  
  6.       return base.MethodA();  
  7.    }  
  8.   
  9. }  
Composition describes an ownership. One class contains an object of another class, in other words a car class has an object of class engine in it.

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

  1. class A{  
  2.   
  3.    public bool MethodA()  
  4.    {  
  5.       return true;  
  6.    }  
  7. }  
  8.   
  9. //composition way  
  10.   
  11. class B  
  12. {  
  13.    A a= new A();  
  14.   
  15.    public bool MethodB()  
  16.    {  
  17.       return a.MethodA();  
  18.    }  
  19. }  
Let’s combine them together and see how they go together:

Now let’s say in our client we invoke a method from class B that in turn invokes MethodA() from class A.
  1. main(string [] args)  
  2. {  
  3.   
  4.    B b= new B();  
  5.   
  6.    Console.writeline(b.MethodB()); // in this case the writeline function would always print Boolean value as true.  
  7.   
  8. }  
If you observe the code above, class B with of the both behaviors composition and inheritance, produce the same results.

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
  1. Class A  
  2. {  
  3.    public string MethodA()  
  4.    {  
  5.       return “Mario”;  
  6.    }  
  7.   
  8. }  
If we compile the existing code as it is, Class B (Inheritence) is extending from class A and therefore would not compile. Because the class A MethodA() no longer returns a boolean value.

Now understand it from the Composition point of view as in the following:
  1. Class B  
  2. {  
  3.   
  4.    A a= new A(); // composition  
  5.   
  6.    Public bool MethodB()  
  7.    {  
  8.       return a.MethodA()==”Mario”?true:false;  
  9.    }  
Class B with composition also fails to compile but the advantages with composition is that with a little tweaking of the code, we still get the expected results and the client does not crash unlike with inheritance.

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.


Similar Articles