Inheritance in java

Definition:-

Inheritance is one of the object-oriented programming pillars. It gives us the feature of code reusability. With the help of inheritance, we can achieve maximum code reusability. It can be used with the help of the "extends" keyword.

Extends Keyword:-

With the help of the "extends" keyword, we can implement the feature of inheritance in our program. when we use extends keyword then we take the property and feature of the parent class to the child class.

Types of inheritance in java:-

  1. Single-level inheritance

  2. Multi-level inheritance

  3. Hierarchical inheritance

Single-level inheritance:-

With the help of single-level inheritance a parent class can be inherited by a single-child class. by this inheritance, properties can be only derived from a single parent class not more than that.

package Inheritance;
class Human
{
    int age;
    public void sleep()
    {
        age =34;
        System.out.println("8 hr sleep is mandatory for student....");
    }
}
class Student extends Human
{
    public void display()
    {

        System.out.println("Age of student is : "+age);
    }
}
public class program1 {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.sleep();
        s1.display();
    }
}

Multi-level inheritance:-

Multi-level inheritance is a type of inheritance in which at least two or more than two classes in involved. in this type of inheritance, multiple base class exits. In multi-level inheritance, one class inherit the feature of a parent class and the newly formed child class becomes the parent class for another new child class.

package Inheritance;
class Animal{
    void eat()
    {
        System.out.println("eating...");
    }
}
class Dog extends Animal{
    void bark()
    {
        System.out.println("barking...");
    }
}
class BabyDog extends Dog{
    void weep()
    {
        System.out.println("weeping...");
    }
}
public class ProgramOfMLI {
    public static void main(String[] args) {
        BabyDog bd = new BabyDog();
        bd.weep();
        bd.bark();
        bd.eat();
    }
}

Hierarchical inheritance:-

Hierarchical inheritance is a type of inheritance in which many sub-classes inherit from a single base class. Newly derived classes inherit the features, methods, etc from one superclass.

package Inheritance;
class Aeroplane
{
    public void takeOff()
    {
        System.out.println("Aeroplane is taking off....");
    }

}
class CargoPlane extends Aeroplane
{
    public void carryGoods()
    {
        System.out.println("CargoPlane carries goods....");
    }
}
class PassengerPlane extends  Aeroplane
{

    public void carryPassenger()
    {
        System.out.println("PassengerPlane carries passenger.....");
    }
}
public class program2 {
    public static void main(String[] args) {
        CargoPlane cp = new CargoPlane();
        cp.takeOff();
        cp.carryGoods();
        PassengerPlane pp = new PassengerPlane();
        pp.takeOff();
        pp.carryPassenger();

    }
}

There are two more types of inheritance which is not supported by java:-

  • Multiple inheritance

  • Hybrid inheritance

Types of methods in inheritance:-

  1. Inherited method

  2. Overridden method

  3. Specialized method

Inherited method:-

The method which comes from the parent class to the child class because of inheritance is called the inherited method.

Overridden method:-

The method which comes from the parent class to the child class and changes the implementation as per the need or requirement of that class is called overridden method.

Specialized method:-

The method which is specific to that particular class is called a specialized method.

That's it for today's blog, more on inheritance will come soon. Thank you for reading this post, leave your constructive feedback in the comment section.

❤️❤️❤️