Inheritance Vs Composition

rupak

Inheritance Vs Composition

This article defines and compares Inheritance and Composition and also provide guidance on usage. Both approaches Inheritance and Composition used to define relationship between the classes.

What is Inheritance?

Inheritance is an OOP concept, and allows the subclass or derived class to inherit the states and behaviors of the base or super-class. A super-class can have any number of sub-classes. But a subclass can have only one superclass.

JAVA does not support multiple inheritance due to Diamond problem in JAVA.(not a part of this article)

The super-class and subclass have “is-a” relationship between them.

Example:-

class Fruit {

      public void peel() {

             System.out.println(“Peeling a fruit”);

      }

}

class Apple extends Fruit {

        public void peel() {

              System.out.println(“Peeling an Apple”);

        }

}

In this example super class is Fruit and subclass is Apple.

UML representation is:-

INHERITANCE REPRESENTATION

The relation is represented by a line with Filled Arrow at the end.

Pros:-

    • Code re-usability:- Whatever code defined in super class can be reused by any of its subclass.
    • Polymorphism:- means super class type variable can hold reference to an object of superclass type or any of its subclass type.
    • Dynamic Binding:- means at run time JVM will decide which method to invoke based on class type of the object.
    • Ease of addition of new subclass:- Suppose need to add new subclass then it can be added just by inheriting the super class in the new class. It won’t affect neither super class nor its existing sub classes.

Cons:-

    • Extra Baggage:- Suppose want to use only one method of a class but not others, then we have to inherit the class which comes with all other unwanted methods and data members as well.
    • Tightly Coupled:- Super class and subclass are tightly coupled. Changes made to super class can break the subclass.
    • Weak encapsulation:- Suppose the return type of peel() method has been changed to “int”, in this case unless until in subclass the overridden method i.e. peel() ‘s return type not changed the code will not compile.

What is Composition?

It is a design technique used to implement “has-a” a relationship between classes. Composition can be achieved by just creating instance variables of other class. By which we can achieve code re-usability as inheritance, but it has its own way of achieving code re-usability.

Example:-

The above example can be modified to – instead of using “extends” need to create a instance variable of Fruit class in Apple Class.

class Fruit {

     public void peel() {

       //…

      }

}

class Apple {

private Fruit fruit = new Fruit();

       public void peel() {

       fruit.peel();

  }

}

UML representation is:-

COMPOSITION REPRESENTATION

The relation is represented by a line with Filled Diamond at the end.

Pros:-

  • Loosely coupled:- Any change to Fruit class does not affect to Apple class. Similarly also adding any method with same signature as Fruit class does not make any affect, and the code will compile perfectly.
  • Code re-usability:- Code re-usability can be achieved same as Inheritance but need to explicitly call the code which needs to be reused.
  • Stronger Encapsulation:- In composition if we change the return type of peel() in Fruit class, there is no need to change anything in Apple class. Without changing anything also the code will compile perfectly and run as well.

Cons:-

  • More Difficult to add new class :- It is less easier to add a new class in composition compared to Inheritance as need to write code explicitly in new class to reuse the existing ode.
  • Performance Cost:- The explicit method call forwarding / delegation approach has a performance cost compared to inheritance single invocation.

Choosing between Composition and Inheritance

  • If the relation between super and subclass is a pure “is a” relationship then better to use Inheritance. In above example(Fruit and Apple class) better to use Inheritance rather than composition.
  • If the interface of super class and subclass needs to or will change through the life cycle of application then better to use composition than Inheritance.                       

        For example :- Employee “is a” person, but in reality Employee is a Role which is played by a person part of               his time. What happens when Employee is unemployed or manager or owner. So this is not a permanent “is a”             relation. Better to use composition over Inheritance.

about the author

rupak