Java - Inheritance in java with example-How to use inheritance in Java?

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Advantage

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.

Important terminology

Super Class/Parent Class: The class whose features are inherited is known as super class(or a base class or a parent class).

Sub Class/Child Class:The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

Reusability: Inheritance supports the concept of “reusability”, i.e. 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. By doing this, we are reusing the fields and methods of the existing class.

How to use inheritance in Java?

The keyword used for inheritance is extends.
Syntax :

class ChildClass-name extends ParentClass-name  
{  
   //methods and fields  
}  

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.


Example:


//Java program to illustrate the  
// concept of inheritance 

// parent class 

class Animal {

public Animal() {

System.out.println("Animal constructor invoked");
}

// the Animal class has two methods

public void eating() {
System.out.println("Animals eating");
}

public void sleeping() {

System.out.println("Animals sleeping");
}

}


// child class

class Lion extends Animal {
public Lion() {
System.out.println("Lion constructor invoked");
}

public void roaring() {

System.out.println("Lion roaring");
}
}

// driver or main class

public class Test {
public static void main(String args[]) {

Lion lion = new Lion();

lion.roaring();
// reusing parent methods
lion.eating();
lion.sleeping();
}
}

Output:

Animal constructor invoked

Lion constructor invoked
Lion roaring
Animals eating
Animals sleeping

In the above example, Lion object can access the field of own class as well as of Animal class i.e. code reusability.




Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.

Note: Multiple inheritance is not supported in Java through class.


Type 1:Single Inheritance


Refers to a child and parent class relationship where a class extends the another class.



Example:


//Java program to illustrate the  
// concept of single inheritance 

// parent class 

class Animal {

public Animal() {

System.out.println("Animal constructor invoked");
}

// the Animal class has two methods

public void eating() {
System.out.println("Animals eating");
}

public void sleeping() {

System.out.println("Animals sleeping");
}

}


// child class

class Lion extends Animal {
public Lion() {
System.out.println("Lion constructor invoked");
}

public void roaring() {

System.out.println("Lion roaring");
}
}

// driver or main class

public class Test {
public static void main(String args[]) {

Lion lion = new Lion();

lion.roaring();
// reusing parent methods
lion.eating();
lion.sleeping();
}
}

Output:

Animal constructor invoked

Lion constructor invoked
Lion roaring
Animals eating
Animals sleeping


Type 2:Multilevel Inheritance

In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.



Example:


//Java program to illustrate the 
// concept of Multilevel inheritance

class Animal {


public Animal() {

System.out.println("Animal constructor invoked");
}

public void eating() {

System.out.println("Animals eating");
}

public void sleeping() {

System.out.println("Animals sleeping");
}

}


class Lion extends Animal {

public Lion() {
System.out.println("Lion constructor invoked");
}

public void roaring() {

System.out.println("Lion roaring");
}
}

class AfricanLion extends Lion {


public AfricanLion() {

System.out.println("AfricanLion constructor invoked");
}

public void africanLionRoaring() {

System.out.println("African Lion roaring");
}
}

// driver or main class

public class Test {
public static void main(String args[]) {

AfricanLion lion = new AfricanLion();

lion.africanLionRoaring();
// reusing parent methods
lion.roaring();
lion.eating();
lion.sleeping();

}

}

Output:


Animal constructor invoked

Lion constructor invoked
AfricanLion constructor invoked
African Lion roaring
Lion roaring
Animals eating
Animals sleeping


Type 3:Hierarchical Inheritance

In Hierarchical Inheritance, one class is inherited by many sub classes.

Example:


//Java program to illustrate the  
// concept of Hierarchical Inheritance 

class Animal {

public Animal() {
System.out.println("Animal constructor invoked");
}

public void eating() {
System.out.println("Animals eating");
}

public void sleeping() {
System.out.println("Animals sleeping");
}

}

class Lion extends Animal {
public Lion() {
System.out.println("Lion constructor invoked");
}

public void roaring() {
System.out.println("Lion roaring");
}
}

class Kangaroo extends Animal {

public Kangaroo() {
System.out.println("Kangaroo constructor invoked");
}

public void jumping() {
System.out.println("Kangaroo jumping");
}
}

// driver or main class
public class Test {
public static void main(String args[]) {

Kangaroo obj = new Kangaroo();
obj.eating();
obj.jumping();
obj.sleeping();

}
}


Output:

Animal constructor invoked
Kangaroo constructor invoked
Animals eating
Kangaroo jumping
Animals sleeping


Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.


This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments