Java Inheritance

Inheritance 

  • It is the mechanism that allows one class to inherit properties from another class without replicating code. It is used heavily in Python, Java, and other programming languages to simplify logic and code reusability
  • It is an essential concept of OOP(Object Oriented Programming) 
  • It is often used to represent Parent classes and Child classes. The Parent class is the class being inherited from which is also called Base Class
  • The child class is the class that inherits from another class which is also called Derived Class.

Advantages of Inheritance:

  1. Code Reusability: Inheritance allows you to reuse the functionality of an existing class in any class that inherits it an indefinite number of times. Without altering the code, you may maintain consistent functionality across all objects of the same type.
  2. Data Hiding: The parent class can be set to keep data private so that derived classes cannot alter it.
  3. Code Structure: Inheritance provides a logical framework to your program that is easy to understand and well structured. 

Java Inheritance

In Java, the Parent class is called Superclass and the inheritor class is called the Subclass. The extends keyword is used to link subclasses with superclasses. super keyword to class inherited methods or the super constructor.

Types of Inheritance in Java:

  • Single Inheritance: One class inherits the properties of another under single inheritance. It allows a derived class to inherit from a single parent class’s properties and behavior.

Code:

Java Code

Java Code:

  class Class1 {
    void print1() {
      System.out.println("Class1 function printed");
    }
  }

class Class2 extends Class1 {
  void print2() {
    System.out.println("Class2 function printed");
  }
}

class solution {
  public static void main(String args[]) {
    Class2 c2 = new Class2();
    c2.print1();
    c2.print2();
  }
}

Output:

Class1 function printed

Class2 function printed

  • Multilevel Inheritance: A derived class inherits a base class, and the derived class also acts as the base class for subsequent classes.

Code:

Java Code

Java Code:

  class Class1 {
    void print1() {
      System.out.println("Class1 function printed");
    }
  }

class Class2 extends Class1 {
  void print2() {
    System.out.println("Class2 function printed");
  }
}

class Class3 extends Class2 {
  void print3() {
    System.out.println("Class3 function printed");
  }
}

class solution {
  public static void main(String args[]) {
    Class3 c3 = new Class3();
    c3.print1();
    c3.print2();
    c3.print3();
  }
}

Output:

Class1 function printed
Class2 function printed
Class3 function printed

  • Hierarchical Inheritance – Hierarchical inheritance is a type of inheritance in which multiple derived classes inherit the properties of the same base class. There are several child classes as well as one parent class.

Code:

Java Code

Java Code:

  class Class1 {
    void print1() {
      System.out.println("Class1 function printed");
    }
  }

class Class2 extends Class1 {
  void print2() {
    System.out.println("Class2 function printed");
  }
}

class Class3 extends Class1 {
  void print3() {
    System.out.println("Class3 function printed");
  }
}

class solution {
  public static void main(String args[]) {
    // Class3 object
    Class3 c3 = new Class3();
    c3.print1();
    //c3.print2(); Will  give error , because it doesn't inherit from class2
    c3.print3();

    System.out.println();

    // Class2 object
    Class2 c2 = new Class2();
    c2.print1();
    c2.print2();
    //c2.print3()  ;  Will  give error , because it doesn't inherit from class3
  }
}

Output:

Class1 function printed
Class3 function printed

Class1 function printed
Class2 function printed

  • Multilevel Inheritance – To simplify and reduce the complexity of language, Java doesn’t support Multilevel Inheritance. Consider a case where we have 3 classes Class1, Class2, and Class3. Now if Class3 inherited Class1 and Class2 and Class1 and Class2 have the same method. The compiler cannot determine which class method should be called or which class method should be called first when calling the method. In Java, if you inherit 2 classes whether you have the same method or different, there will be a compile-time error.

Code:

Java Code

Java Code:
  class Class1 {
    void print() {
      System.out.println("Class1 will get printed");
    }
  }

class Class2 {
  void print() {
    System.out.println("Class2 will get printed");
  }
}

class Class3 extends Class1, Class2 {}
class solution {
  public static void main(String[] args) {
    Class3 c3 = new Class3();
    c3.print();
  }
}

Output: Compile Time Error

  • Interfaces: Interfaces can provide a default implementation of methods in Java 8, so default methods are supported. A class can also implement multiple interfaces.

Code:

Java Code

Java Code:
  interface Class1 {
    default void print() {
      System.out.println("Print class 1");
    }
  }

interface Class2 {
  default void print() {
    System.out.println("Print class 2");
  }
}

class solution implements Class1, Class2 {

  public void print() {
    Class1.super.print();
    Class2.super.print();
  }

  public static void main(String[] args) {
    solution s1 = new solution();
    s1.print();
  }
}

Output:

Print class 1

Print class 2

  • Hybrid Inheritance – Hybrid inheritance is the result of combining single and multiple inheritances. We can’t use classes in Java to implement multiple inheritances. If we write Class1 extends 2, 3, then the Java compiler throws an error. Interfaces can be used to achieve hybrid inheritance. Because interfaces allow for multiple inheritances.

Code:

Java Code

Java Code:
  interface Class1 {
    public void print1();
  }
interface Class2 {
  public void print2();
}
interface Class3 {
  public void print3();
}

class solution implements Class2, Class3 {
  public void print1() {
    System.out.println("Print Class1");
  }
  public void print2() {
    System.out.println("Print Class2");
  }
  public void print3() {
    System.out.println("Print Class3");
  }

  public static void main(String[] args) {
    solution obj = new solution();
    obj.print1();
    obj.print2();
    obj.print3();

  }

}

Output:

Print Class1
Print Class2
Print Class3

Special thanks to Shreyas Vishwakarma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article