A process in which one class utilizes the property of another class is called Inheritance. The class that's utilizing the property of another class is called a child class, and therefore the other is called parent class.
It facilitates the code reusability so that a class has to write only the unique features and the rest of the common properties(Data Members) and functionalities(Member Functions) can be extended from another class. Child Class: The class that extends the features of another class is known as child class, subclass or derived class. Parent Class:
The class whose properties and functionalities are inherited by another class is known as a parent class, superclass, or Base class. The process of defining a new class based on an existing class that extends its common data members and methods is called inheritance. Inheritance allows us to reuse code, it improves reusability in your java application.
Note: The main advantage of Inheritance is code reusability, the code that is already present in base class need not be rewritten in the child class.
Syntax: To inherit the properties of a class we use extends keyword. Here class B is child class and class A is parent class. Class B is inheriting the properties and methods of Class A.
Important Note!!
The derived class can inherit all the data members and member functions that are declared as public or protected. If the data members or member functions of the superclass are declared as private then the derived class cannot use them directly. The private members are only accessible in its own class. To access private members out of class, getter and setter are used.
Single inheritance: In single inheritance, a derived class is created from one parent class. It refers to a child and parent class relationship where a class extends another class. Syntax:
Let's take an example. Suppose we have a parent class "Vehicle" and child class "Car". Child class will acquire the features of the parent class. As shown in the main method we have created an object of the child class, this object will call the methods of both classes (Child & Parent)
Multilevel inheritance: In multilevel inheritance, one class can inherit from the child class. Hence, the child class becomes the parent class for the new one. As shown in the below diagram Class C is a child of Class B and B is a child class of Class A.
Hierarchical Inheritance: In hierarchical inheritance, more than one classes extend one class. In the below diagram Class B, C, and D inherit the same class A.
Hybrid Inheritance: Hybrid inheritance is a combination of Single and Multiple inheritances in a single program. As per the below example, all the public and protected members of Class A are inherited into Class D, via Class B and Class C.
Note: Java doesn't support hybrid/Multiple inheritances
A SuperClass constructor is called by using super keyword. The super keyword refers to the superclass, immediately above the calling class in the hierarchy. To access the data members or methods of the parent class super keyword is used.
It facilitates the code reusability so that a class has to write only the unique features and the rest of the common properties(Data Members) and functionalities(Member Functions) can be extended from another class. Child Class: The class that extends the features of another class is known as child class, subclass or derived class. Parent Class:
The class whose properties and functionalities are inherited by another class is known as a parent class, superclass, or Base class. The process of defining a new class based on an existing class that extends its common data members and methods is called inheritance. Inheritance allows us to reuse code, it improves reusability in your java application.
Note: The main advantage of Inheritance is code reusability, the code that is already present in base class need not be rewritten in the child class.
Syntax: To inherit the properties of a class we use extends keyword. Here class B is child class and class A is parent class. Class B is inheriting the properties and methods of Class A.
class B extends A { }
Inheritance Example:
In the following example, we have a base class Shape and a subclass Circle. Since class Circle extends the width and height properties and area() method from the base class, we need not declare these properties and methods in the subclass. Here we have width, height, and area() method which are common to all the shapes so we have declared them in the base class, this way the child classes like Circle, Rectangle and Triangle do not need to write this code and can be used directly from the base class.class Shape { int width = 20; int height = 30; public void area(){ System.out.println("Shape"); } } class Circle extends Shape{ double radius = 40; public static void main(String args[]){ Circle obj = new Circle(); System.out.println(obj.width); System.out.println(obj.height); System.out.println(obj.radius); obj.area(); } }Output:
20 30 40 ShapeSo based on the above example we can say that Circle IS-A Shape. In Java, a child class has an IS-A relationship with the parent class. This is inheritance is known as the IS-A relationship between the child and parent class.
Important Note!!
The derived class can inherit all the data members and member functions that are declared as public or protected. If the data members or member functions of the superclass are declared as private then the derived class cannot use them directly. The private members are only accessible in its own class. To access private members out of class, getter and setter are used.
Types of Inheritance:
There are different types of inheritance in Java:- Single inheritance
- Multiple inheritances
- Multilevel inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Single inheritance: In single inheritance, a derived class is created from one parent class. It refers to a child and parent class relationship where a class extends another class. Syntax:
//Parent Class class Parent { //methods and fields } //Child Class class childClass extends Parent { //methods and fields }
class Vehilce { public void engine() { System.out.println("engine..."); } } class Car extends Vehicle { public void start() { System.out.println("start..."); } } public class ExecuteClass { public static void main(String args[]) { Car c = new Car(); c.engine(); c.start(); } }Multiple inheritances: In multiple inheritance one class extending more than one class, which means One child class has two parent classes. As per the above diagram, Class C extends Class A and Class B both.
Multilevel inheritance: In multilevel inheritance, one class can inherit from the child class. Hence, the child class becomes the parent class for the new one. As shown in the below diagram Class C is a child of Class B and B is a child class of Class A.
Hierarchical Inheritance: In hierarchical inheritance, more than one classes extend one class. In the below diagram Class B, C, and D inherit the same class A.
Hybrid Inheritance: Hybrid inheritance is a combination of Single and Multiple inheritances in a single program. As per the below example, all the public and protected members of Class A are inherited into Class D, via Class B and Class C.
Note: Java doesn't support hybrid/Multiple inheritances
Constructors and Inheritance:
When you create an object of a child class, it's constructor is called, it by default invokes the superclass class default constructor. In inheritance, constructors are called in a top-down approach.A SuperClass constructor is called by using super keyword. The super keyword refers to the superclass, immediately above the calling class in the hierarchy. To access the data members or methods of the parent class super keyword is used.
class ParentClass{ //Parent class constructor ParentClass(){ System.out.println("Constructor of Parent"); } } class ChildClass extends ParentClass{ ChildClass(){ System.out.println("Constructor of Child"); } public static void main(String args[]){ //Creating the object of child class new ChildClass(); } }Output:
Constructor of Parent Constructor of Child
Inheritance and Method Overriding:
To declare the same method in child class which is already present in the parent class then this is called method overriding. In this case, when we call the method from the child class object, the child class version of the method is called. However, we can call the parent class method using the super keyword as I have shown in the example below:class ParentClass{ //Parent class constructor ParentClass(){ System.out.println("Constructor of Parent"); } void disp(){ System.out.println("Parent Method"); } } class ChildClass extends ParentClass{ ChildClass(){ System.out.println("Constructor of Child"); } void disp(){ System.out.println("Child Method"); //Calling the disp() method of parent class super.disp(); } public static void main(String args[]){ //Creating the object of child class ChildClass obj = new ChildClass(); obj.disp(); } }