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 inheritance
- Hybrid inheritance
- Hierarchical inheritance
Example of Inheritance:
addition of a+b is:11
Here, class B is the derived class which inherit the property(add method) of the base class A.
The composition also provides code reusability but the difference here is we do not extend the class for this.
Example of Composition:
Lets take example of Library.
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:
|1.||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.|
|2.||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.|
|3.||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.|
|4.||Inheritance cannot extend final class.||Whereas composition allows code reuse even from final classes.|
|5.||It is an is-a relationship.||While it is a has-a relationship.|
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Association, Composition and Aggregation in Java
- Composition in Java
- Difference between Inheritance and Interface in Java
- Inheritance and constructors in Java
- Comparison of Inheritance in C++ and Java
- Java and Multiple Inheritance
- Interfaces and Inheritance in Java
- Inheritance in Java
- Using final with Inheritance in Java
- Object Serialization with Inheritance in Java
- Java | Inheritance | Question 1
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 4
- Java | Inheritance | Question 5
- Java | Inheritance | Question 9
- Java | Inheritance | Question 7
- Java | Inheritance | Question 8
- Java | Inheritance | Question 9
- Delegation vs Inheritance in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.