Inheritance | OOps |

Inheritance Overview:

  • Inheritance is a fundamental concept in object-oriented programming (OOP).

  • Inheritance in java is a mechanism by which one class is allowed to inherit the features(fields and methods) of another class.
    • It allows a class to inherit the properties and methods of another class, known as the superclass or base class.
    • The class that inherits these properties and methods is called the subclass or derived class.
    • Inheritance promotes code reuse, enhances modularity, and enables polymorphism.


    Real time  Ex : Parents & Children






    To establish inheritance in Java, you use the extends keyword.

    The subclass declaration follows the superclass declaration, and the extends keyword is used to specify the superclass.

    Example: class SubClass extends SuperClass { }





    • How to implement inheritance in Java ?

    -Java provides extends keyword to inherit the features of a class. 


    • Important terminologies in Java inheritance

    1. Subclass/Child class : The class that inherits the features of other class is known as subclass, also known as child class, derived class or extended class.
    2. Superclass/Parent class : The class whose features are being inherited is known as super class, also known as parent class or base class.
    3. re-usability : mechanism that facilitates to reuse fields and methods of the existing class into the new class is known as reusability.
      When you create a new class, you can use the same fields and methods already defined in the previous class. This is called reusability.

        

    • Is-A relationship in java

        Is-A relationship in java represents Inheritance. It is implemented in Java through keywords extends (for class inheritance) and implements (for interface implementation).

    For example:


    class A {
      // statements
    }
    The Java compiler will treat the above code like this:
    class A extends Object{
      // statements.
    }



    types of inheritance


    • Single inheritance
    • Multilevel inheritance
    • Hierarchical inheritance
    ------------------------------- NOT Supported ------------------------------------
    • Multiple inheritance(Supported using interfaces, not by classes)
    • Hybrid inheritance(Mix of two or more of above inheritance types)


     






    Single level Inheritance in Java

    When a class inherits only one another class, we call it single inheritance or single level inheritance. This is the simplest form of inheritance as it involves only two classes. Below program shows an example of single inheritance.


    class A {
       void add(int a, int b) {
          System.out.println(a +"+" +b+" = "+(a+b));
        }  
     }
     
     class B extends A {
       void subtract(int a, int b) {
          System.out.println(a +"-" +b+" = " +(a-b));
        }  
       public static void main(String args []) {
          B obj = new B();
          obj.add(5,10);
          obj.subtract(20,10);
        }  
     }


    Multilevel Inheritance in Java

    When three or more than three classes inherits in same chain or level, we call it multilevel inheritance. The number of classes in multilevel inheritance is not limited to three classes, it can go up to any number of classes in the same level or inheritance chain.


    class A {
       void add(int a, int b) {
          System.out.println(a +"+" +b+" = "+(a+b));
        }  
     }
     
     class B extends A {
       void subtract(int a, int b) {
          System.out.println(a +"-" +b+" = " +(a-b));
        }
     }
      
     class C extends B {
       void multiply(int a, int b) {
          System.out.println(a +"*" +b+" = " +(a*b));
        }    
       public static void main(String args []) {
          C obj = new C();
          obj.add(5,10);
          obj.subtract(20,10);
          obj.multiply(10,20);
        }  
     }


    Hierarchical Inheritance in Java

    When two or more than two classes inherits a single class, we call it hierarchical inheritance. In java a class can be inherited by any number of classes, so in hierarchical inheritance, there can be any number of child classes, it's not limited to two classes only. The program below shows an example of hierarchical inheritance.

    class A {
       void add(int a, int b) {
          System.out.println(a +"+" +b+" = "+(a+b));
        }  
     }
     
     class B extends A {
       void multiply(int a, int b) {
          System.out.println(a +"*" +b+" = " +(a*b));
        }
     }
      
     class C extends A {
      void divide(int a, int b) {
          System.out.println(a +"/" +b+" = " +(a/b));
        }
     }
       
     class Test {  
       public static void main(String args []) {
          B obj = new B();    
          obj.add(5,10);
          obj.multiply(20,10);
          
          C obj2 = new C();
          obj2.add(20,30);
          obj2.divide(20,10);
        }  
     }



    Multiple Inheritance in Java Using support to interface


    When a class inherits the features from more than one parent, we call it multiple inheritance. Multiple inheritance in java is not supported using classes, which means a class can not extend more than one class.


    Multiple inheritance in java is supported through interfaces only, which means a class can inherit the features(specially default and static methods from java 8 onwards) from more than one interfaces. Java allows a class to implement any number of interfaces. The program below shows an example of multiple inheritance.

    interface A {
       public void print();
       default void add(int a, int b) {
          System.out.println(a +"+" +b+" = "+(a+b));
        }  
     }
     
     interface B {
         public void print();
         default void subtract(int a, int b) {
           System.out.println(a +"-" +b+" = " +(a-b));
        }
     }
      
     class C implements A, B {
        public void print() {
           System.out.println("Testing multiple inheritance in Java");
         }
        public static void main(String args []) {      
          C obj = new C();
          obj.print();
          obj.add(20,10);
          obj.subtract(20,10);
        }
     }


    Why Java does not support multiple inheritance through classes ?

    This is a design decision taken by the creators of java. To make java simple and reduce the complexity, multiple inheritance is not supported in java.

    ambiguity is one of the main reason why java does not support multiple inheritance.



    Hybrid Inheritance in Java

    Hybrid inheritance, as the name itself suggests, it is a mix of two or more inheritance types given above.
    hybrid inheritance is a mix of hierarchical and multiple inheritance but it's not limited to that only, it may contain the mix of any other inheritance types as well.







    Comments

    Popular Posts