DEV Community

loading...

Inheritance in Java

ritul120 profile image Ritul Jain ・Updated on ・2 min read

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.

Discussion (3)

pic
Editor guide
Collapse
nikolahristov profile image
Niki Hristov

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change: code block with no colors example ... to specify the language: code block with colors example More details in our editor guide!

Collapse
michelemauro profile image
michelemauro

Keep studying, you are on the right path! Let me share with you just a couple of notes.

Where did you find the names of "Multilevel", "Hierarchical" and "Hybrid" inheritance? usually, they are just possible organizations of classes, they don't have a name.

Saying that Java doesn't support multiple inheritance is not entirely correct: Java doesn't support it for classes, but a class can implement multiple Interfaces, and Interfaces themselves can do the same.

Collapse
ritul120 profile image
Ritul Jain Author

Thank you for providing this valuable feedback.