Difference between Inheritance and Composition in Java
Last Updated :
12 Jul, 2025
When we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. In doing this, we can reuse the fields and methods of the existing class without having to write them ourself.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Types of Inheritance are:
- Single inheritance
- Multi-level inheritance
- Multiple inheritances
- Hybrid inheritance
- Hierarchical inheritance
Example of Inheritance:
Java
// Java Program Demonstrating
// Inheritance
class A {
int a, b;
public void add(int x, int y){
a = x;
b = y;
System.out.println("addition of a + b is:"
+ (a + b));
}
}
// Class B Inheriting Class A
class B extends A
{
public void sum(int x, int y){
add(x, y);
}
public static void main(String[] args){
B b1 = new B();
b1.sum(5, 6);
}
}
Output:
addition of a+b is:11
Here, class B is the derived class which inherit the property(add method) of the base class A.
Composition
The Composition also provides code reusability but the difference here is we do not extend the class for this.
Example of Composition: Let us take an example of the Library.
Java
// Java program to illustrate
// the concept of Composition
import java.util.*;
// class book
class Book {
public String title;
public String author;
Book(String t, String a)
{
this.title = t;
this.author = a;
}
}
// Library class contains
// list of books.
class Library {
// reference to refer to the list of books.
private final List<Book> books;
Library(){
books = new ArrayList<>();
}
public void add(Book b){
books.add(b);
}
public List<Book> getTotalBooksInLibrary(){
return books;
}
}
class Main
{
public static void main(String[] args)
{
// Creating the Objects of Book class.
Book b1 = new Book("EffectiveJ Java","Joshua Bloch");
Book b2 = new Book("Thinking in Java","Bruce Eckel");
Book b3 = new Book("Java: The Complete Reference","Herbert Schildt");
// Creating the list which contains the
// no. of books.
Library library = new Library();
library.add(b1);
library.add(b2);
library.add(b3);
List<Book> bks = library.getTotalBooksInLibrary();
for (Book bk : bks) {
System.out.println("Title : " + bk.title + " and "
+ " Author : " + bk.author);
}
}
}
Output:
Title : EffectiveJ Java and Author : Joshua Bloch
Title : Thinking in Java and Author : Bruce Eckel
Title : Java: The Complete Reference and Author : Herbert Schildt
Difference between Inheritance and Composition
Inheritance | Composition |
---|
In inheritance, we define the class which we are inheriting(super class) and most importantly it cannot be changed at runtime | Whereas in composition we only define a type which we want to use and which can hold its different implementation also it can change at runtime. Hence, Composition is much more flexible than Inheritance. |
Here we can only extend one class, in other words more than one class can't be extended as java do not support multiple inheritance. | Whereas composition allows to use functionality from different class. |
In inheritance we need parent class in order to test child class. | Composition allows to test the implementation of the classes we are using independent of parent or child class. |
Inheritance cannot extend final class. | Whereas composition allows code reuse even from final classes. |
It is an is-a relationship. | While it is a has-a relationship. |
Similar Reads
Difference between Inheritance and Interface in Java Java is one of the most popular and widely used programming languages. Java has been one of the most popular programming languages for many years. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). I
3 min read
Difference Between Aggregation and Composition in Java Aggregation and composition describe the type of relationships between objects when communicating with each other, this might be used in low-level design to depict associations between objects. In this article, we are going to discuss the differences between Aggregation and Composition in Java progr
6 min read
Difference Between Object and Instance in Java The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class. Object in JavaThe object is an instance of a class. A class is a blueprint or template that describes
3 min read
Difference Between Abstract Class and Interface in Java In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use
9 min read
Comparison of Inheritance in C++ and Java The purpose of inheritance is the same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating an âis-aâ relationship. The following examples will demonstrate the differences between Java and C++ that provide support for inheritance. 1) In Java, all classes inherit fr
4 min read
Difference between Final and Abstract in Java In this article, the difference between the abstract class and the final class is discussed. Before getting into the differences, lets first understand what each of this means. Final Class: A class which is declared with the "Final" keyword is known as the final class. The final keyword is used to
4 min read