Inheritance in java

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass.

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Inheritance

Java Inheritance is a mechanism in which one class acquires the property of another class. In Java, when an “Is-A” relationship exists between two classes, we use Inheritance. The parent class is called a super class and the inherited class is called a subclass. The keyword extends is used by the sub class to inherit the features of super class.

Inheritance is important since it leads to the reusability of code.

Types of inheritance:

1) single inheritance

2) multi level inheritance

3) Hierarchical Inheritance

4)multiple inheritance

5)hybrid inheritance

Single inheritance:

In Single Inheritance one class extends another class (one class only).

Single Inheritance in Java
Single Inheritance

In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.

Class Parent
{
   public void methodA()
   {
     System.out.println("Base class method");
   }
}

Class Child extends Parent
{
   public void methodB()
   {
     System.out.println("Child class method");
   }
   public static void main(String args[])
   {
     Child son = new Child();
     son.methodA(); //calling super class method
     son.methodB(); //calling local method
  }
}

Multi-Level Inheritance in Java

In Multi-Level Inheritance in Java, a class extends to another class that is already extended from another class. For example, if there is a class A that extends class B and class B extends from another class C, then this scenario is known to follow Multi-level Inheritance.

We can take an example of three classes, class Vehicle, class Car, and class SUV. Here, the class Vehicle is the grandfather class. The class Car extends class Vehicle and the class SUV extends class Car.

Multilevel Inheritance in Java
Class Vehicle  //grant parent
{
   public void method1()
   {
     System.out.println("Class Vehicle method");
   }
}
Class Car extends Vehicle   //parent
{
public void method2()
{
System.out.println("class Car method");
}
}
Class Suv extends Car    //child
{
   public void method3()
   {
     System.out.println("class Suv method");
   }
   public static void main(String args[])
   {
     Suv innova = new Suv();
     innova.method1(); //calling grand parent class method
     innova.method2(); //calling parent class method
     innova.method3(); //calling local method
  }
}
output
 class vehicle method
 class car method
 class Suv method

Multiple Inheritance in Java

Defining derived class from numerous base classes is known as ‘Multiple Inheritance’. In this case, there is more than one superclass, and there can be one or more subclasses. 

Multiple inheritances are available in object-oriented programming with C++, but it is not available in Java. 

Java developers want to use multiple inheritances in some cases. Fortunately, Java developers have interface concepts expecting the developers to achieve multiple inheritances by using multiple interfaces. 

Ex: class Myclass implements interface1, interface2,…. 

Why Multiple Inheritance is not supported in Java?
Let's consider a case in Inheritance. Consider a class A, class B and class C. Now, let class C extend class A and class B. Now, consider a method read() in both class A and class B. The method read() in class A is different from the method read() in class B. But, while inheritance happens, the compiler has difficulty in deciding on which read() to inherit. So, in order to avoid such kind of ambiguity, multiple inheritance is not supported in Java. 

Hierarchical Inheritance
When more than one classes inherit a same class then this is called hierarchical inheritance. For example class B, C and D extends a same class A. Lets see the diagram representation of this:
Hierarchical Inheritance

When more than one classes inherit a same class then this is called hierarchical inheritance. For example class B, C and D extends a same class A. Lets see the diagram representation of this:

class Parent
{
   public void methodA()
   {
      System.out.println("method of Class parent");
   }
}
class Child1 extends Parent
{
   public void methodB()
   {
      System.out.println("method of Class child1");
   }
}
class Child2 extends Parent
{
  public void methodC()
  {
     System.out.println("method of Class Child2");
  }
}
class Child3 extends Parent
{
  public void methodD()
  {
     System.out.println("method of Class Child3");
  }
}
class Hierarchical
{
  public static void main(String args[])
  {
     Child1  ram = new Child1();
     Child2  sita = new Child2();
     Child3  pazham = new Child3();
     //All classes can access the method of class A
     ram.methodA();
     sita.methodA();
     pazham.methodA();
  }
}

output
method of class parent
method of class parent
method of class parent

Hybrid Inheritance in Java

Hybrid Inheritance in Java is a combination of inheritance. In this type of Inheritance, more than one kind of inheritance is observed. For example, if we have class A and class B that extend class C and then there is another class D that extends class A, then this type of Inheritance is known as Hybrid Inheritance.

Why? Because we clearly observe that there is two kinds of inheritance here- Hierarchical and Single Inheritance.

In the diagram shown below, we see another example of Hybrid Inheritance.

 Hybrid Inheritance in Java 

References;

https://www.guru99.com/java-class-inheritance.html

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

https://beginnersbook.com/2013/05/java-inheritance-types/

https://beginnersbook.com/2013/10/hierarchical-inheritance-java-program/

https://www.mygreatlearning.com/blog/inheritance-in-java/

Leave a comment

Design a site like this with WordPress.com
Get started