Overview of Inheritance and Types of Inheritance


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Inheritance and Types of Inheritance </aside>

Inheritance in Java


  1. Inheritance in Java

    1. Inheritance is the most powerful feature of object-oriented programming. It allows us to inherit the properties of one class into another class.

    2. Inheritance

      1. Inheritance is a mechanism of driving a new class from an existing class. The existing (old) class is known as base class or super class or parent class. The new class is known as a derived class or sub class or child class. It allows us to use the properties and behavior of one class (parent) in another class (child).

      2. A class whose properties are inherited is known as parent class and a class that inherits the properties of the parent class is known as child class. Thus, it establishes a relationship between parent and child class that is known as parent-child or Is-a relationship.

      3. Suppose, there are two classes named Father and Child and we want to inherit the properties of the Father class in the Child class. We can achieve this by using the extends keyword.

        //inherits the properties of the Father class  
        class Child extends Father {
          // functionality
        }
        

        image.png


  2. When we should use inheritance?

    1. Inheritance provides the reusability of code especially when there is a large scale of code to reuse. It also establishes the relationship between different classes that is known as a Is-a relationship. We can also use it if we want to achieve method overriding.

  3. Points to Remember

    1. Constructor cannot be inherited in Java.
    2. Private members do not get inherited in Java.
    3. Cyclic inheritance is not permitted in Java.
    4. Assign parent reference to child objects.
    5. Constructors get executed because of super() present in the constructor.

  4. Types of Inheritance

    1. Java supports the following four types of inheritance:
      1. Single Inheritance

      2. Multi-level Inheritance

      3. Hierarchical Inheritance

      4. Hybrid Inheritance.

        image.png


  5. Single Inheritance

    1. In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.

      image.png

    2. In the above figure, Employee is a parent class and Executive is a child class. The Executive class inherits all the properties of the Employee class.

    3. Implementation of the single inheritance mechanism in a Java program.

      class Employee {
        float salary = 34534 * 12;
      }
      
      public class Executive extends Employee {
        float bonus = 3000 * 6;
      
        public static void main(String args[]) {
          Executive obj = new Executive();
          System.out.println("Total salary credited: " + obj.salary);
          System.out.println("Bonus of six months: " + obj.bonus);
        }
      }
      
    4. Output:

      Total salary credited: 414408.0
      Bonus of six months: 18000.0
      

  6. Multi-level Inheritance

    1. Multi-level inheritance occurs when a class is derived from another class, and that derived class further inherits from another class. In simpler terms, it's like a chain of inheritance where one class inherits from another, and that inherited class inherits from yet another class.

      image.png

    2. In the above figure, the class Marks inherits the members or methods of the class Students. The class Sports inherits the members of the class Marks. Therefore, the Student class is the parent class of the class Marks and the class Marks is the parent of the class Sports. Hence, the class Sports implicitly inherits the properties of the Student along with the class Marks.

    3. Implementation of the multi-level inheritance mechanism in a Java program.

      //super class  
      class Student {
        int reg_no;
      
        void getNo(int no) {
          reg_no = no;
        }
      
        void putNo() {
          System.out.println("registration number= " + reg_no);
        }
      }
      
      // intermediate sub class
      class Marks extends Student {
        float marks;
      
        void getMarks(float m) {
          marks = m;
        }
      
        void putMarks() {
          System.out.println("marks= " + marks);
        }
      }
      
      // derived class
      class Sports extends Marks {
        float score;
      
        void getScore(float scr) {
          score = scr;
        }
      
        void putScore() {
          System.out.println("score= " + score);
        }
      }
      
      public class MultilevelInheritanceExample {
        public static void main(String args[]) {
          Sports ob = new Sports();
          ob.getNo(0987);
          ob.putNo();
          ob.getMarks(78);
          ob.putMarks();
          ob.getScore(68.7);
          ob.putScore();
        }
      }
      
    4. Output:

      registration number = 0987
      marks= 78.0
      score= 68.7