Skip to main content

Inheritance in Java

Inheritance

  • It explains with its name itself, by using this feature we can able to use all properties of one class into another class using extends keyword. 
  • Key terms in this concept are parent class or superclass or base class and child class or subclass or derived class. 

Types of Inheritance

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance 
  5. Hybrid Inheritance 

1. Single Inheritance

  • It will have only 1 parent or superclass and 1 child class. Properties of the parent class are accessible in the child class using the extends keyword
  • Using this concept we can able to use methods from the parent class without creating an object for the parent class. 

Sample Program

Class name: TutorialInheritance.java
                                                                                                                                                                
package oops;   
                                                                                                
class ParentClass{                                                                                            
void testSum(int number1,int number2) {                                               
int sum = number1 + number2;                                                        
System.out.println("Two number added in parent class : " + sum); 
}                                                                                                                
}                                                                                                                        

class ChildClass extends ParentClass{                                                                
void testSubtract(int number1,int number2) {                                             
int sub = number1 - number2;                                                              
System.out.println("Two number subtracted in child class : " + sub); 
}                                                                                                                    
}         
                                                                                                                   
class TutorialInheritance{                                                                                    
public static void main(String[] args){                                                        
ChildClass childClass = new ChildClass();                                         
childClass.testSum(10,5);                                                                    
childClass.testSubtract(10,5);                                                              
}                                                                                                                   
}                                                                                                                           
                                                                                                                                                               

Output :
Two number added in parent class: 15
Two number subtracted in child class: 5

Program Explanation

In the above sample program, 
I have created 1 parent class named "ParentClass" with method "testSum" which adds 2 numbers
1 child class named "ChildClass" with method "testSubtract" which substracts 2 numbers

One class to access these both classes named "TutorialInheritance"
In this class, we are going to access the above methods inside the main method
We can able to access both methods using the same object created for the child class       
ChildClass childClass = new ChildClass();
childClass.testSum(10,5);
childClass.testSubtract(10,5);    

There is no need to create multiple objects to access different methods from different classes.  

2. Multilevel Inheritance

This is similar to Single Inheritance and the only difference is that it will contain 3 classes. Example Class A, Class B, and Class C. For Class B, Class A will be the parent class and for Class C, Class B will be the Parent class.

Sample Program

Class name: TutorialInheritance.java
                                                                                                                                                                
package oops;

class ParentClass{
void testSum(int number1,int number2) {
int sum = number1 + number2;
System.out.println("Two number added in parent class : " + sum);
}
}

class ChildClass extends ParentClass{
void testSubtract(int number1,int number2) {
int sub = number1 - number2;
System.out.println("Two number subtracted in child class : " + sub);
}
}

class AnotherClass extends ChildClass{
void testMultiply(int number1,int number2) {
int sub = number1 * number2;
System.out.println("Two number subtracted in anther child class : " + sub);
}
}

class TutorialInheritance{
public static void main(String[] args){
AnotherClass anotherClass = new AnotherClass();
anotherClass.testSum(10,5);
anotherClass.testSubtract(10,5);
anotherClass.testMultiply(10,5);
}
}
                                                                                                                                                                

Output :
Two number added in parent class: 15
Two number subtracted in child class: 5
Two number subtracted in anther child class: 50
                                                                                                       
Program Explanation

In the above sample program, 
I have created 1 parent class named "ParentClass" with method "testSum" which adds 2 numbers
One Child class that extends PatentClass named "ChildClass" with method "testSubtract" which subtracts 2 numbers

Another class named "AnotherClass" with the method "testMultiply" which multiplies 2 numbers. This class extends ChildClass. So, this class can have access to both ChildClass and ParentClass. So, We can able to access all methods using the object reference of the class "AnotherClass"
ChildClass extends ParentClass -- ChildClass aquires properties of ParentClass 
AnotherClass extends ChildClass -- AnotherClass aquires properties of both ChildClass and ParentClass

The child class can have access to the parent class but the parent class don't have access to the child class 

3. Multiple Inheritance

    In java, we don't have multiple inheritances due to its complexity. We can't able to inherit methods from different classes. The problem arises in this concept when two-parent classes contain the same method. The compiler will get confused n which method should be used so it will show a compiler error. Java compiler doesn't have the ability to tackle this issue. So, this type is not applicable to Java. 

4. Hierarchical Inheritance

In this type, there will be 2 child classes which will inherit the same parent class

Sample Program

Class name: TutorialInheritance.java
                                                                                                                                                                
package oops;

class ParentClass{
void testSum(int number1,int number2) {
int sum = number1 + number2;
System.out.println("Two number added in parent class : " + sum);
}
}

class ChildClassOne extends ParentClass{
void testSubtract(int number1,int number2) {
int sub = number1 - number2;
System.out.println("Two number subtracted in child class : " + sub);
}
}

class AnotherClassTwo extends ParentClass{
void testMultiply(int number1,int number2) {
int sub = number1 * number2;
System.out.println("Two number Multiplied in child class : " + sub);
}
}

class TutorialInheritance{
public static void main(String[] args){
ChildClassOne childClassOne = new ChildClassOne();
AnotherClassTwo childClasstwo = new AnotherClassTwo();
childClassOne.testSum(10,5);
childClassOne.testSubtract(10,5);
childClasstwo.testSum(100,50);
childClasstwo.testMultiply(10,5);
}
}
                                                                                                                                                                

Output:
Two number added in parent class: 15
Two number subtracted in child class: 5
Two number added in parent class: 150
Two number Multiplied in child class: 50

Program Explanation

In the above sample program,
In this type, I have created one parent class having the method t sum 2 numbers
We had 2 child classes named "ChildClassOne" and "AnotherClassTwo" which extends the same parent class


Suggest us to improve us!!!

Comments