Open In App

Favoring Composition Over Inheritance In Java With Examples

Last Updated : 22 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Favoring Composition over Inheritance is a principle in object-oriented programming (OOP). Classes should achieve polymorphic behavior and code reuse by their composition rather than inheritance from a base or parent class. To get the higher design flexibility, the design principle says that composition should be favored over inheritance. 

Inheritance should only be used when subclass ‘is a’ superclass. Don’t use inheritance to get code reuse. If there is no ‘is a’ relationship, then use composition for code reuse.

Reasons to Favour Composition over Inheritance in Java and OOP:

  1. The fact that Java does not support multiple inheritances is one reason for favoring composition over inheritance in Java. Since you can only extend one class in Java, but if you need multiple features, such as reading and writing character data into a file, you need Reader and Writer functionality. It makes your job simple to have them as private members, and this is called Composition.
  2. Composition offers better test-ability of a class than Inheritance. If one class consists of another class, you can easily construct a Mock Object representing a composed class for the sake of testing. This privilege is not given by inheritance.
  3. Although both Composition and Inheritance allow you to reuse code, one of Inheritance’s disadvantages is that it breaks encapsulation. If the subclass depends on the action of the superclass for its function, it suddenly becomes fragile. When super-class behavior changes, sub-class functionality can be broken without any modification on its part.
  4. In the timeless classic Design Patterns, several object-oriented design patterns listed by Gang of Four: Elements of Reusable Object-Oriented Software, favor Composition over Inheritance. Strategy design pattern, where composition and delegation are used to modify the behavior of Context without touching context code, is a classical example of this. Instead of getting it by inheritance, because Context uses composition to carry strategy, it is simple to have a new implementation of strategy at run-time.
  5. Another reason why composition is preferred over inheritance is flexibility. If you use Composition, you are flexible enough to replace the better and updated version of the Composed class implementation. One example is the use of the comparator class, which provides features for comparison.

Inheritance :

Inheritance is the design strategy to implement a relationship between objects in object-oriented programming. The extends keyword is used to implement inheritance in Java.

class Person {
 String title;
 String name;
 int age;
}

class Employee extends Person {
 int salary;
 String title;
}

In the above example, The Employee “is a” Person or inherits from Person. All inheritance relationships are “is-a” relationships. The Employee also shadows the title property from Person, meaning Employee. title will return the Employee’s title, not the Person.

Composition :

In object-oriented programming, the composition is the architecture strategy for executing a relationship between objects. Java composition is done using instance variables from other objects. 

class Person {
 String title;
 String name;
 int age;

 public Person(String title, String name, String age) {
    this.title = title;
    this.name = name;
    this.age = age;
 }

}

class Employee {
 int salary;
 private Person person;

 public Employee(Person p, int salary) {
     this.person = p;
     this.salary = salary;
 }
}

Person p = new Person ("Mr.", "Kapil", 25);
Employee kapil = new Employee (p, 100000);

The composition is typically “has a” or “uses a” relationship. Here the Employee class has a Person. It does not inherit from Person but instead gets the Person object passed to it, which is why it “has a” Person.

Composition over Inheritance

Now say you want to create a Manager type, so you end up with the below syntax, which is not allowed in java (multiple Inheritance is not allowed in java) :

//multiple inheritance not allowed

class Manager extends Person, Employee {
}

now we have to favor composition over Inheritance using the below syntax:

Class Manager {
 public string title;
 public Manager(Person p, Employee e)
 {
    this.title = e.title;
 }
}

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads