Difference between inheritance and composition in Java

DifferencesJavaObject Oriented Programming

In computer programming, the concept of reusable code refers to the utilisation of previously developed software in the construction of new software. Reusability of code is recognised as an essential component of productive functionality. Establishing associations between classes is one method that object-oriented programming uses to encourage this.

In object-oriented programming, there are two primary ways to construct these relationships: inheritance and composition.

In object-oriented programming (OOP), inheritance refers to the process through which an object can take on the properties of one or more other objects. In OOP, it is one of the most powerful concepts for establishing code reusability. When employing inheritance, a new class can be built by establishing parent-child connections with pre-existing classes. This allows for the creation of new subtypes of existing classes.

An alternative to class inheritance is something called "object composition." Composition refers to the process of utilising one thing within another object. Composition is a new form of relationship that may be used between classes that is made possible by OOP.

Because it is simple to develop complicated classes by leveraging previously established, well-designed classes as components, you will find that there are numerous situations in which you will want to use an object within another class as a field. The term for this process is "composition."

Even though the functionality provided by inheritance and composition is roughly similar in many contexts, object composition is considered to be a superior reuse approach than class inheritance. Read through this article to find out more about Inheritance and Composition and how these two concepts are different from each other.

What is Composition?

Composition, which is also known as a "has-a relationship", is another type of relationship that OOP offers for classes to have with one another.

Composition is necessary when one item's properties have to be included into another object in order for the relationship to make sense. In order to create a new class that is composed of pre-existing classes, an object from each of the existing classes must be specified as a member of the new class.

Composition is a fancy name for nesting one thing inside of another, and it can be explained rather simply. There are several situations in which you would want to use an object that is already defined as a field in another class. In composition, an object is used when working within a class.

Object composition, in contrast to class inheritance, is defined dynamically at runtime through the process of objects obtaining references to the objects of other classes. Composition is beneficial because it provides a better way to use an object without violating the internal information of the object. This is another benefit of composition.

Example of Composition

public class Student {
}
public class College {
   private Student student;
   public College() {
      this.student = new Student();
   }
}

What is Inheritance?

When it comes to implementing code reusability in object-oriented programming, one of the most powerful tools at your disposal is inheritance. It is a type of capability that allows one thing to gain the properties of one or more other objects through the use of another object.

Java allows you to build classes that derive their properties from other classes. It means that you specialize a class in order to construct an "is-a relationship" between the classes, which ultimately results in a strong coupling between the base class and the derived classes.

Because new classes are generated from already existing classes when inheritance is implemented, code reusability is increased. Inheritance of classes makes it much simpler to make changes to an implementation that is being reused. However, there are also certain drawbacks associated with the inheritance of classes. Inheritance is defined during compile time, so you cannot alter the implementations that are inherited from the parent classes during runtime.

The nature of inheritance can be single-level, multi-level, hierarchical, multi-level, or hybrid. Inheritance can also take the form of hybrid inheritance.

Example of Inheritance

class Animal{
   String name="Orio";
}
public class Dog extends Animal{
   String type="Dog";
   public static void main(String args[]){
      Dog p=new Dog();
      System.out.println("Name:"+p.name);
      System.out.println("Type:"+p.type);
   }
}

Output

Name:Orio
Type:Dog

Comparison between Composition and Inheritance

The following table highlights the major differences between Composition and Inheritance in object-oriented programming −

Basis of Comparison
Composition
Inheritance
Relationship
It is a "has-a" kind of circumstance.
It's a case of "is-a."
Functionality
We can evaluate the functioning of the classes that we are using without having to be concerned with determining whether or not they are the parent or the child classes.
It is not possible to test a child class without first testing its parent class.
It is not possible to test a child class without first testing its parent class.
Because of composition, it is possible to reuse the code even in the final classes.
Inheritance cannot be used to extend the functionality of the final class.
Significance
Simply declaring a type that we want to use in composition allows it to store several implementations, each of which can have unique behaviors depending on the context of when they are called.
In inheritance, we are responsible for defining the class that will become our "superclass," and this definition is immutable once it has been used.
Class Combining
Composition gives users the flexibility to mix features and capabilities from multiple classes into a single entity.
Java does not support multiple inheritances, which means that several classes cannot be extended.

Conclusion

A composition is a kind of aggregation in which ownership is assumed rather than explicitly stated. Take into consideration the following objects − one associated with a school and one associated with a classroom. It is impossible for the classroom object to exist without the school object first. Hence, these two objects are said to be interdependent. As such, we can call it a composition in its own right. To put it another way, something is said to be a composition if it is impossible for it to exist apart from the whole.

Composition, often known as a "has-a" relation, is an additional relationship between classes that object-oriented programming provides. Composition is required if the traits of one thing need to be merged in some way into the features of another thing because of the link between the two things. It is necessary to declare the object of each class as a member of the current class in order to generate a new class from the existing parent class.

raja
Updated on 28-Jul-2022 11:37:32

Advertisements