Inheritance in Java

ritul120

Ritul Jain

Posted on March 25, 2021

Inheritance in Java

Java and Inheritance

Inheritance is a mechanism in java by which one object inherits the features(methods and fields) of the parent object. Inheritance is an important pillar of OOPs(Object Oriented Programming systems). Inheritance represents IS-A relationship.

Terminology

  • Sub Class: The class that inherits features of another class is known as sub class. It is also called extended class, derived class, or child class. Sub class can add new methods and fields as well.
  • Super Class: The class that is inherited by other class is known as super class. It is also known as base class or parent class.

Syntax:

class derived-class extends base-class
{
    //methods and fields
}
Enter fullscreen mode Exit fullscreen mode

Example

Inheritance

Implementation

class Person{  
 String name= "John";  
}  
class Employee extends Person{  
   float salary = 10000;  
   public static void main(String args[]){  
     Employee e =new Employee();  
     System.out.println("Name of Employee is:"+ e.name);  
     System.out.println("Salary of Employee is:"+ e.salary);  
   }  
} 
Enter fullscreen mode Exit fullscreen mode

Types of Inheritance

  • Single Inheritance: It refers to a child and parent class relationship where a class extends the another class.
    single

  • Multilevel Inheritance: It refers to a child and parent class relationship where a class extends the child class. For example class C extends class B and class B extends class A.
    multilevel

  • Hierarchical Inheritance: It refers to a child and parent class relationship where more than one classes extends the same class. For example, classes B, C & D extends the same class A.
    hierarchical

  • Multiple Inheritance: It refers to the concept of one class extending more than one classes, which means a child class has two parent classes. For example class C extends both classes A and B. Java doesn’t support multiple inheritances with classes.Though we can achieve multiple inheritances through interfaces in java.
    multiple

  • Hybrid Inheritance: Combination of more than one types of inheritance in a single program. For example class B & C extends class A and another class D extends class B then this is a hybrid inheritance example because it is a combination of single and hierarchical inheritance.
    hybrid

Conclusion

I hope this article is helpful to you in learning Inheritance in Java.

💖 💪 🙅 🚩
ritul120
Ritul Jain

Posted on March 25, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Dynamic Dispatch and Dispatch Tables
javascript Dynamic Dispatch and Dispatch Tables

May 14, 2021

Inheritance in Java
java Inheritance in Java

March 25, 2021