Inheritance in Java

Inheritance is the process by which one object acquires the properties of another object. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.

 

 

Excepting Object, which has no super-class, every class has one and only one direct super-class (single inheritance). In the absence of any other explicit super-class, every class is implicitly a subclass of 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 super-class.

Constructors are not members, so they are not inherited by sub-classes, but the constructor of the super-class can be invoked from the subclass.

Important terminology:

  • Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the super-class fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
  • The keyword used for inheritance is extends.

Function Overloading : Defining function again with 

  • Same Name.
  • Different Arguments (number, sequence, type).
  • Same Class.

Function Overriding : Defining function again with 

  • Same Name.
  • Same Arguments (number, sequence, type).
  • Base and Derived Class.

From the view of derive class, there are 3 categories of methods in the base class :

  • Constructors:

    1.To call constructors of base class, “super” keyword is used.
    2.syntax : super(<args>);
    3.Note : only constructors of derived class can call constructors of base class (firstline).

  • Overridden Functions

    To call overridden functions of base class, syntax is : super.functionName(<args>);

  • Remaining/Normal Functions

    1.To call remaining functions, syntax is :
    2.functionName(<args>);

Types of Inheritance in Java :

Below are the different types of inheritance which is supported by Java.

  1. Single Inheritance : In single inheritance, sub-classes inherit the features of one super-class. In image below, the class A serves as a base class for the derived class B.

Example:

class Animal{
   void eat(){System.out.println("eating");}
class Dog extends Animal{
   void barko{System.out.println("barking");}
class Testlnheritance{
   public static void main(String args[]){
   Dog d = new Dog();
   d.bark();
   d.eat();
Output:
barking
Eating

2 .  Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.

class Animal{
   void eat(){System.out.println("eating");}
}
class Dog extends Animal{
   void bark(){System.out.println("barking");}
}
class BabyDog extends Dog{
   void weepo{System.out.println("weeping");}
}
class Testlnheritance2{
   public static void main(String args[]){
   BabyDog d = new BabyDog();
   d.weep();
   d.bark();
   d.eat();
 }
}
Output:
weeping
barking
Eating

3.  Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a super-class (base class) for more than one sub class.In below image, the class A serves as a base class for the derived class B,C and D.

 

class Animal{
   void eat(){System.out.println("eating");}
class Dog extends Animal{
   void barko{System.out.println("barking");}
class Cat extends Animal{
   void meow(){System.out.println("meowing");}
class Testlnheritance3{
   public static void main(String args[]){
   Cat c = new Cat();
   c.meow();
   c.eat();
}}
Output:
meowing
Eating

4.Multiple Inheritance (Through Interfaces) : In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and B.

 

 

 

5. Hybrid Inheritance(Through Interfaces) : It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.


 

Find The Working Example Of Inheritance on P3Lang GitHub Repo.

 

 

 

 

 

%d bloggers like this: