You must have heard or read "Favour Composition over Inheritance" from may people, many blogs and books. Here goes my opinion on the same...
Anyone who had worked on .NET framework to build applications has extensvily used both Inheritance as well as Composition. When a class object is being referenced by the another class object, it forms the parent/child hierarchy, where the object being referenced is the object of sub-class and the object which is holding the the referenced object is the super-class object, and this parent-child hierarchy is refereed as Inheritance. When a class uses another object to provide some or all of its functionalities then it's refereed as Composition.
Whenever Inheritance is being discussed, knowing or unknowingly Composition comes into the picture. Lets understand both the concepts:
Many say that go with composition when you need multiple functionalities in a class, as you can't inherit from more than one class in .NET. But what I believe is, if you have only this reason (a class can't inherit from multiple base classes) to go for Composition then you don't have to as we can achieve this by making class implement multiple interfaces.
Both Inheritance & Composition are useful while developing applications. Just that you need to know when to use what? Below are the steps I follow while selecting Inheritance or Composition.
Anyone who had worked on .NET framework to build applications has extensvily used both Inheritance as well as Composition. When a class object is being referenced by the another class object, it forms the parent/child hierarchy, where the object being referenced is the object of sub-class and the object which is holding the the referenced object is the super-class object, and this parent-child hierarchy is refereed as Inheritance. When a class uses another object to provide some or all of its functionalities then it's refereed as Composition.
Whenever Inheritance is being discussed, knowing or unknowingly Composition comes into the picture. Lets understand both the concepts:
- Both supports code reuse
- Composition - The purpose of it is to make wholes out of parts using code reuse.
- Inheritance - The purpose of it is to make parent-child hierarchy to reuse code (common code defined in parent class).
- "Is A" and "Has A" Relationship
- Composition is a "Has A" relationship as the the class owns the object of another class.
- Inheritance is a "Is A" relationship as the sub-class is also a type of super-class hence the object of super class can be used as reference object to hold the object of sub-class.
- Coupling
- In case of composition the class object is been used directly by the another class, so the reusable class is robust.
- In case of Inheritance, the super-class doesn't know anything about the sub-classes so any changes goes into the sub-classes there is no change in the behaviours of super-class but the sub-class knows about the super-class as it inherits all its behaviours so any changes goes into the super-class the sub-classes may get affected which makes a tight coupling between them.
- Test-ability
- Composition provided more flexibility for testing as its is easy class to mock objects used inside the class.
- In case of Inheritance you will need the base class (super-class). Since the derived classes (sub-classes) are tightly coupled with the base class (super-class) it become slightly difficult to mock derived class objects.
- In case of TDD mocking Composition are far much faster and easier than mocking derived classes.
Many say that go with composition when you need multiple functionalities in a class, as you can't inherit from more than one class in .NET. But what I believe is, if you have only this reason (a class can't inherit from multiple base classes) to go for Composition then you don't have to as we can achieve this by making class implement multiple interfaces.
Both Inheritance & Composition are useful while developing applications. Just that you need to know when to use what? Below are the steps I follow while selecting Inheritance or Composition.
- First Approach:
- Use Inheritance when you want to reuse the class without modifying any of its behaviours. Basically If sub-class is adding functionality to the base class go with Inheritance, if sub-class is removing behaviours from the super-class, question inheriting from super-class.
- Second Approach:
- Use Inheritance when you have "Is A" relationship like, a car "is a" vehicle.
- Use Composition when you have "Has A" relationship like, a car "has an" engine.