Inheritance In Java | Java Tutorial
Inheritance is a process where one class inherits the properties of another class.
Let’s say we have two classes namely Parent Class and Child Class. The child class is also known as Derived Class. As per the above definition, the Child class inherits the properties of the Parent Class. The main purpose of Inheritance is to obtain Code Reusability. We can achieve run time polymorphism with inheritance. We will see what is run-time polymorphism in a few minutes.
Check this link to learn what is a Class and Object
Assume we have a Class named Laptop, Apple MacBook Pro, Lenovo Yoga. Apple MacBook Pro and Lenovo Yoga classes extend the Laptop Class to inherit the properties of the Laptop Class.
We will see a detailed explanation with some example programs about Inheritance in the post related to Inheritance.
In inheritance, we use two keywords namely extends and implements
Extends:
We use the extends keyword in Java to allow the child class to inherit all the properties (data members and methods) of the parent class and in addition to these, we can also create new data members and methods. If the properties are private then the child class cannot inherit those properties from the parent class.
class ChildClass extends ParentClass { }
Technically, we say that child class has an IS-A relationship with the parent class
class QA extends Employee { }
As per the IS-A relationship, we can say QA IS-A Employee
Employee Class:
package classFourOops.inheritance; public class Employee { String name = "Rajkumar"; }
QA Class:
package classFourOops.inheritance; public class QA extends Employee{ String fullName = "Rajkumar SM"; public static void main(String [] args){ QA objName = new QA(); System.out.println(objName.name); System.out.println(objName.fullName); } }
In the above example, the QA object can access the properties of its own as well as the Employee class.
Implements:
We use implements keyword in Java to inherit the properties from an interface. Interfaces cannot be extended by the classes.
We will see this in the Interface section.
Types of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
There are two more inheritances as Multiple and Hybrid Inheritance which are supported through interface only. We will see those in the Interface section.
Single Inheritance:
Single Inheritance in Java refers to a child and parent class relationship. In this one class extends another class say Class B extends Class A
package classFourOops.singleInheriance; public class ClassA { void methodOneClassA(){ System.out.println("I am a Method One of ClassA"); } }
package classFourOops.singleInheriance; public class ClassB extends ClassA{ void methodOneClassB(){ System.out.println("I am a Method One of ClassB"); } }
package classFourOops.singleInheriance; public class InheritanceTest { public static void main(String args[]){ // Class B extends Class A // Here I am creating an instance of ClassB ClassB obj = new ClassB(); // Using object of ClassB, I can call methods of ClassA and ClassB.. Its just because ClassB extends ClassA obj.methodOneClassA(); obj.methodOneClassB(); } }
Output:
I am a Method One of ClassA I am a Method One of ClassB
Multilevel Inheritance:
Multilevel Inheritance in Java refers to a child and parent class relationship. In this a class extends a child class say Class C extends Class B and Class B extends Class A
package classFourOops.multipleInheriance; public class ClassA { void methodOneClassA(){ System.out.println("I am a Method One of ClassA"); } }
package classFourOops.multipleInheriance; public class ClassB extends ClassA{ void methodOneClassB(){ System.out.println("I am a Method One of ClassB"); } }
package classFourOops.multipleInheriance; public class ClassC extends ClassB{ void methodOneClassC(){ System.out.println("I am a Method One of ClassC"); } }
package classFourOops.multipleInheriance; public class InheritanceTest { public static void main(String args[]){ // Class B extends Class A // Class C extends Class B // Here I am creating an instance of ClassC ClassC obj = new ClassC(); // Using object of ClassC, I can call methods of ClassA, ClassB and ClassC.. // Its just because ClassB extends ClassA and ClassC extends ClassB obj.methodOneClassA(); obj.methodOneClassB(); obj.methodOneClassC(); } }
Output:
I am a Method One of ClassA I am a Method One of ClassB I am a Method One of ClassC
Hierarchical Inheritance:
Hierarchical Inheritance in Java refers to a child and parent class relationship. In this more than one class extends the same class say Class B, Class C extends Class A
package classFourOops.hierarchialInheriance; public class ClassA { void methodOneClassA(){ System.out.println("I am a Method One of ClassA"); } }
package classFourOops.hierarchialInheriance; public class ClassB extends ClassA{ void methodOneClassB(){ System.out.println("I am a Method One of ClassB"); } }
package classFourOops.hierarchialInheriance; public class ClassC extends ClassA{ void methodOneClassC(){ System.out.println("I am a Method One of ClassC"); } }
package classFourOops.hierarchialInheriance; public class InheritanceTest { public static void main(String args[]){ // Class B extends Class A // Class C extends Class A // Here I am creating an instance of ClassC ClassC obj = new ClassC(); // Using object of ClassC, I can call methods of ClassA and ClassC.. // Its just because ClassC extends ClassA // Here ClassB cant call methods of ClassB because there is no relation between ClassC and ClassB obj.methodOneClassA(); obj.methodOneClassC(); } }
Output:
I am a Method One of ClassA I am a Method One of ClassC
Multiple Inheritance:
Multiple Inheritance is not possible in Java. In this a child extends two parents classes say Class C extends Class A and Class B.
Hybrid Inheritance:
Combination of more than one type of inheritance.
Must Read: Java Tutorial