Difference between Inheritance and Composition in Java
In this article, we are going to discuss the difference between Inheritance and Composition in Java. This article is not limited to the comparison between both terms; along with comparison, we will also discuss both terms separately.
Hope this article will be informative to you and give you sufficient information about inheritance, composition, and their comparison. We will try to make the article easy to read and understand and provide you the concept in an easy language.
Let's first see the brief description of inheritance and composition in Java.
Inheritance is an important part of OOPs (Object Oriented programming system). In Java, it is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It shows the IS-A relationship, which is also called as parent-child relationship. As an instance, the car is a vehicle, the cat is an animal, and many more.
In Java, with the help of inheritance, we can create new classes that are built upon existing classes. When we inherit from an existing class, the methods and fields of the parent class can be reused. We can also add new methods and fields to our current class.
Inheritance is used for method overriding in order to achieve the runtime polymorphism. It is also used for code reusability. Code reusability is a way that facilitates us to reuse the fields and methods of the existing class when we create a new class. We can use the same fields and methods already defined in the previous class.
The extends keyword represents the creation of a new class that is derived from an existing class. The word "extends" means to increase the functionality.
In Java, the inherited class is called a parent or superclass, whereas the new class is called a child or subclass.
In Java, there are three types of inheritance on the basis of class: single inheritance, multilevel inheritance, and hierarchical inheritance. Multiple inheritance and hybrid inheritance can be supported by using interface in Java. It is not supported through class in Java.
For more information, you can visit to inheritance in Java.
Example of Inheritance in Java
Let's understand the concept of inheritance in Java using an example. It will explain the concept more clearly. Here, the Parent class is School, and the child class is Student. The School class contains the student's name, and the child class contains the student's rollno.
In the above example, the object of Student class can access the field of own class as well as of School class i.e. code reusability.
Inheritance and Composition both are design techniques. The Composition is a way to design or implement the "has-a" relationship whereas, the Inheritance implements the "is-a" relationship. The "has-a" relationship is used to ensure the code reusability in our program. In Composition, we use an instance variable that refers to another object.
Although both inheritance and composition provide the code reusability, the difference between both terms is that in composition, we do not extend the class. The composition relationship of two objects is possible when one object contains another object, and that object is fully dependent on it. The composition represents the part of relationship. For instance, A house has a living room (living room is a part of house), A person has a heart (heart is a part of the human body), and many more.
Composition has various benefits like it allows code reusability, it is helpful in achieving the multiple inheritance, it provides better test-ability of a class. It also allows us to easily replace the composed class implementation with a better and improved version. Using composition concept, we can dynamically change the behavior of our program by changing the member objects at run time.
Let's understand the concept of composition in Java using an example. It will explain the concept more clearly.
For more information, you can visit to composition in Java.
Inheritance v/s Composition
Now, let's see the comparison chart between inheritance and composition in Java. We are comparing both terms on the basis of some characteristics.
The composition gives us the better way of reusing the code, and at the same time, it protects the class that we are reusing from any of its clients. Inheritance is mainly important when we have to create a class from the same family.
Next TopicDifference between