Difference between Inheritance and Composition in Java - GeeksforGeeks

22 Jul, 2021
Inheritance: 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 inheritanceMulti-level inheritanceMultiple inheritancesHybrid inheritanceHierarchical inheritance
Single inheritance
Multi-level inheritance
Multiple inheritances
Hybrid inheritance
Hierarchical inheritance
Example of Inheritance: 
Java
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 extends A {    public void sum(int x, int y)    {        add(x, y);    }     // Driver Code    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.io.*;import java.util.*; // class bookclass Book {     public String title;    public String author;     Book(String title, String author)    {         this.title = title;        this.author = author;    }} // Library class contains// list of books.class Library {     // reference to refer to the list of books.    private final List<Book> books;     Library(List<Book> books)    {        this.books = books;    }     public List<Book> getTotalBooksInLibrary()    {         return books;    }} // main methodclass GFG {    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.        List<Book> books = new ArrayList<Book>();        books.add(b1);        books.add(b2);        books.add(b3);         Library library = new Library(books);         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:
 
sagar0719kumar
Inheritance
Java-Object Oriented
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
StringBuilder Class in Java with Examples
Comparator Interface in Java with Examples
Generics in Java
Functional Interfaces in Java
Java Programming Examples
HashMap get() Method in Java