Inheritance in Java4 min read

Introduction

Inheritance in Java can be defined as the process where one object acquires the properties of another. This is one of the fundamental mechanisms of Object-Oriented Programming. With the use of inheritance, the information is made manageable in a hierarchical order. When we talk about inheritance, the most commonly used keyword would be extended and implements.

In Java, inheritance refers to the ability to create new classes from existing ones. You can use the methods and properties of an existing class when you inherit from it. You can also add additional methods and properties to your current class. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

  • In Java, inheritance is used for extending, classes. Inheritance allows new classes to be derived from existing classes. The new class is called a subclass or derived class or child class. A subclass can add new behavior and properties.
  • The term inheritance refers to the fact that one class can inherit a part or all of its structure and behavior from another class.
  • The class from which a new class is derived is called a superclass or base class or parent class.
  • A subclass is declared by specifying a superclass using the extends clause in its header.
  • A subclass includes all the members of its superclass except those that have been declared private in the superclass.
  • A major advantage of inheritance is that once we have created a superclass that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses.
public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}

public class Dog extends Mammal{
}

Now, based on the above example, In Object Oriented terms, the following are true:

  • The Animal is the superclass of the Mammal class.
  • The Animal is the superclass of the Reptile class.
  • Mammal and Reptile are subclasses of the Animal class.
  • The dog is the subclass of both Mammal and Animal classes.

Now, if we consider the IS-A relationship, we can say:

  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal
  • Hence Dog IS-A Animal as well

Different types of inheritance

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance

Single Inheritance

Single Inheritance is a process of deriving a class from multiple base classes. That is several super interfaces and a single subclass.

Inheritance in Java - single inheritance
Single Inheritance
class Animal{
  void eat(){
    System.out.println("eating...");
  }  
}  

class Dog extends Animal{
  void bark(){
    System.out.println("barking...");
  }  
}

class TestInheritance{
  public static void main(String args[]){  
    Dog d=new Dog();  
    d.bark();  
    d.eat();  
  }
}

Output :

barking...
eating...

Multiple Inheritance

Multiple Inheritance is a process of deriving a class from multiple base classes. That is several super interfaces and a single subclass. This is not possible directly. But can be achieved using the interface. In Multiple Inheritance, we can extend one superclass and implement another superclass.

Inheritance in Java - multiple inheritance
Multiple Inheritance

Hierarchical Inheritance

Hierarchical Inheritance is a process of deriving multiple classes from the single base class that is a single superclass and several sub-classes.

Hierarchical inheritance
class Animal{  
  void eat(){
    System.out.println("eating...");
  }  
} 

class Dog extends Animal{  
  void bark(){
    System.out.println("barking...");
  }  
} 

class Cat extends Animal{  
  void meow(){
    System.out.println("meowing...");
  }  
} 

class TestInheritance3{  
  public static void main(String args[]){  
    Cat c=new Cat();  
    c.meow();  
    c.eat();  
    //c.bark();//C.T.Error  
  }
}  

Output :

meowing...
eating...

Multilevel Inheritance

Multilevel Inheritance is a process of deriving a class from another derived class, that is one superclass one intermediate class, and one subclass.

Multilevel inheritance
class Animal{  
  void eat(){
    System.out.println("eating...");
  }  
} 

class Dog extends Animal{  
  void bark(){
    System.out.println("barking...");
  }  
} 

class BabyDog extends Dog{  
  void weep(){
    System.out.println("weeping...");
  }  
} 

class TestInheritance2{  
  public static void main(String args[]){  
    BabyDog d=new BabyDog();  
    d.weep();  
    d.bark();  
    d.eat();  
  }
} 

Output :

weeping...
barking...
eating...

Share:

Leave a Reply