Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
Terms used in Inheritance
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
The syntax of Inheritance
{
//methods and fields
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // Java Program to Illustrate // Invocation of Constructor // Calling Without Usage of // super Keyword // Class 1 // Super class class Base { // Constructor of super class Base() { // Print statement System.out.println( "Base Class Constructor Called "); } } // Class 2 // Sub class class Derived extends Base { // Constructor of sub class Derived() { // Print statement System.out.println( "Derived Class Constructor Called "); } } // Class 3 // Main class public class Wrap { // Main driver method public static void main(String[] args) { // Creating an object of sub class // inside main() method Derived d = new Derived(); // Note: Here first super class constructor will be // called there after derived(sub class) constructor // will be called } } |
Output:
1 2 | Base Class Constructor Called Derived Class Constructor Called |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | // Java Program to Illustrate Invocation // of Constructor Calling With Usage // of super Keyword // Class 1 // Super class class Base { int x; // Constructor of super class Base(int _x) { x = _x; } } // Class 2 // Sub class class Derived extends Base { int y; // Constructor of sub class Derived(int _x, int _y) { // super keyword refers to super class super(_x); y = _y; } // Method of sub class void Display() { // Print statement System.out.println("x = " + x + ", y = " + y); } } // Class 3 // Main class public class Wrap { // Main driver method public static void main(String[] args) { // Creating object of sub class // inside main() method Derived d = new Derived(10, 20); // Invoking method inside main() method d.Display(); } } |
Output:
1 | x = 10, y = 20 |
super() can also be used to call a parent class method.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // Java Program to Illustrate Invocation // of Constructor Calling With Usage // of super Keyword // Class 1 // Super class class Base { public void Display() { System.out.println("Display form Base Class"); } } // Class 2 // Sub class class Derived extends Base { // Method of sub class public void Display() { // Display from Base class super.Display(); System.out.println("Display form Derived Class"); } } // Class 3 // Main class public class Wrap { // Main driver method public static void main(String[] args) { // Creating object of sub class // inside main() method Derived d = new Derived(); // Invoking method inside main() method d.Display(); } } |
Output:
1 2 | Display form Base Class Display form Derived Class |
Types of inheritance
On the basis of class, there can be three types of inheritance in java:
- Single
- Multilevel
- Hierarchical
When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Single Inheritance
In Java, Single Inheritance is the concept that a class can only inherit from a superclass. This means that a subclass cannot inherit from more than one parent class. This is a design feature of Java to avoid complexity and inconsistency that can arise from inheriting from multiple classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //Super Class class Animal { public void eat() { System.out.println("This animal eats food."); } } //SubClass class Dog extends Animal { public void bark() { System.out.println("This dog barks."); } } //Main class public class Wrap { public static void main(String[] args) { Dog mydog = new Dog(); mydog.eat(); mydog.bark(); } } |
Multilevel Inheritance
Multilevel inheritance in Java is a mechanism by which a class can inherit from another class, which in turn inherits from another class. This forms a chain of derived classes, where each class inherits the characteristics of its parent class.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //Class 1 // First Parent class class Animal { public void eat() { System.out.println("This animal eats food."); } } //Class 2 // Second Parent class class Mammal extends Animal { public void breathe() { System.out.println("This mammal breathes air."); } } //Class 3 // Child of both the classes class Dog extends Mammal { public void bark() { System.out.println("This dog barks."); } } //Main class public class Wrap { public static void main(String[] args) { Dog mydog = new Dog(); mydog.eat(); mydog.breathe(); mydog.bark(); } } |
Output:
1 2 3 | This animal eats food. This mammal breathes air. This dog barks. |
Hierarchical Inheritance
Hierarchical inheritance in Java is a type of inheritance in which multiple subclasses inherit from the same parent class. This creates a tree structure, where a parent class has many child classes that inherit from it.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //Class 1 // First Parent class class Animal { public void eat() { System.out.println("This animal eats food."); } } //Class 2 class Cat extends Animal { public void meow() { System.out.println("This cat meows."); } } //Class 3 class Dog extends Animal { public void bark() { System.out.println("This dog barks."); } } //Main class public class Wrap { public static void main(String[] args) { Dog mydog = new Dog(); mydog.eat(); mydog.bark(); Cat mycat = new Cat(); mycat.eat(); mycat.meow(); } } |
Output:
1 2 3 4 | This animal eats food. This dog barks. This animal eats food. This cat meows. |