What is Inheritance in Java?

When we talk about inheritance in java, It means the capability of one class to derive properties from another class.

The most important advantage of inheritance is that code reusability means we can the same codebase to every program. Let’s discuss how?

What are the base and Derived Class?

Let’s discuss it with an example for better understanding, suppose the class car inherits from another class automobiles which itself inherits from another class called vehicles.

This property of object-oriented languages, the class inheritance, lets you derive new classes(called derived classes) from the old one, with the derived class inheriting the properties, including the methods of the old class known as bae class.

Once a base is written and debugged, it can be used in various situations without having to redefine the old class, you can add new properties to the derived class, and you are able to redefine an inherited class member functions.

In short, the new derived class is called the derived class or subclass and the existing class is called a base class or superclass. And it is said that the subclass or derived class may be inherited from the base class.

Types of inheritance in Java?

Inheritance may take place in many forms which are being discussed below:

1.Single Inheritance in java:

When a class inherits, another class, it is known as single inheritance. In the example given below, the Actor class inherits the Bollywood class, so there is a single inheritance.

class Bollywood{  
    void launch(){System.out.println("acting...");}  
    }  
   class Actor extends Bollywood{  
    void go(){System.out.println("struggling...");}  
    }  
    public class TestInheritance{  
    public static void main(String args[]){  
    Actor d=new Actor();  
    d.go();  
    d.launch();  
    }}  

Output : struggling...
         acting...

2.Multiple Inheritance:

When there is any chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, Parents class inherits the Actor class which again inherits the Bollywood class, so there is a multilevel inheritance.

class Bollywood{  
void launch(){System.out.println("acting...");}  
}  
class Actor extends Bollywood{  
void go(){System.out.println("struggling...");}  
}  
class Parents extends Actor{  
void stop(){System.out.println("career...");}  
}  
public class TestInheritance2{  
public static void main(String args[]){  
Parents d=new Parents();  
d.stop();  
d.go();  
d.launch();  

}}

Output : career...
         struggling...
         acting...

3. Hierarchical Inheritance:

When the two or more classes inherit a single class, it is known as hierarchical inheritance. In the example given below, Actor and Opposite classes inherit the Bollywood class, so there is hierarchical inheritance.

class Bollywood{  
void launch(){System.out.println("acting...");}  
}  
class Actor extends Bollywood{  
void go(){System.out.println("struggling...");}  
}   
class Opposite extends Bollywood{  
void tackel(){System.out.println("tackeling...");}  
}  
public class TestInheritance3{  
public static void main(String args[]){  
Opposite c=new Opposite();  
c.tackel();  
c.launch();  
//c.go();//C.T.Error  
}} 

Output : tackeling...
         acting...

Why Method Overriding in Java?

It mainly allows a general class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods.

Overriding and Inheritance also the main part of the key to successfully applying polymorphism is understanding that the superclasses and subclasses form a hierarchy which moves from lesser to greater specialization.

Super Keyword in Java

In Java, the super keyword is used for the only reference variable if we create some instances of a subclass, an instance of the parent class is created implicitly which is referred by a super reference variable.

Usage of Java super Keyword

  • super can be used to just refer immediate parent class instance variable.
  • super can be used to just invoke an immediate parent class method.
  • super() can be used to just invoke immediate parent class constructor.