In this article Inheritance in Java is a tool that allows you to take all the features of an existing class into a new class without duplicating it.
This is a very important feature, which reduces the length of the code and can be organized properly.
Inheritance in Java:
Definition:-
“Inheritance is a mechanism by which a new class is created from an old class.”
By this the properties of old class can be used in new class. To use the properties of the old class in the new class, the old class has to be inherited.
In inheritance, the old class is called base class or parent class or super class, the property of which class is taken. The new class is called child class or derived class or sub class, through which the property is taken.
Terms used in Inheritance:
Class: A class is a group of objects that have common properties. It is a template or blueprint from which objects are created.
Sub Class/Child Class: Subclass is a class that inherits another class. It is also called a derived class, extended class or child class.
Super Class/Parent Class: Super class is the class from which a subclass inherits the features. It is also called base class or parent class.
Reusability: As the name specifies, reusability is a mechanism that allows you to reuse the fields and methods of an existing class while creating a new class. You can use the same fields and methods already defined in the previous class.
The syntax of Java Inheritance
class Subclass-name extends Super-class-name
{
//methods and fields
}
The extends keyword indicates that you are creating a new class that derives from an existing class. “Extends” means to increase the functionality.
Types of inheritance in java
There are five types of inheritance
- Single
- Multiple
- Multilevel
- Hierarchical
- Hybrid
Single inheritance in java:
Definition:
“A derived class with only one base class is known as single inheritance.”
In this, the property of one class is taken by another class. It has only one super class and one sub class.
Syntax :-
class P
{
—–
—–
}
Class Q extends P
{
——-
——-
}
// Program of Single Inheritance:-
class Base
{
public void show()
{
System.out.println(“Hi!…”);
}
}
class Derived extends Base
{
public void Display()
{
System.out.println(“Hello!…”);
}
}
class Hello
{
public static void main(String [] args)
{
Derived obj=new Derived();
obj.show();
obj.Display();
}
}
Output :-
Hi!…
Hello!…
Multiple inheritances in java:
Definition:
“A derived class with several base classes is called multiple inheritance.”
In this the property of more than one class is inherited by one class. It has more than one super class and one sub class.
Java does not support multiple inheritance.
Syntax:-
Class P
{
}
Class Q
{
}
Class R extends P , extends Q
{
}
Multilevel inheritance in java:
Definition:
“The mechanism of deriving a class from another derived class is known as multilevel inheritance.”
In this a class inherits another class and the class which inherits the class becomes a sub class and the same sub class is inherited by another class. Similarly, all classes inherit each other.
Syntax:-
Class P
{
}
Class Q extends P
{
}
Class R extends Q
{
}
// Multilevel inheritance Program:
class X
{
public void showX()
{
System.out.println(“Base Class method”);
}
}
class Y extends X
{
public void showY()
{
System.out.println(“First Derived Class method”);
}
}
class Z extends Y
{
public void showZ()
{
System.out.println(“Second Derived Class method”);
}
public static void main(String [] args)
{
Z obj = new Z();
obj.showX(); //calling grand parent class method
obj.showY(); //calling parent class method
obj.showZ(); //calling local method
}
}
O/P:
Base Class method
First Derived Class method
Second Derived Class method
Hierarchical inheritance in java:
Definition:
“Several derived classes with only one base class is known as Hierarchical inheritance.”
In this, the property of one class is taken by more than one class. Hierarchical inheritance has a base class and more than one sub class. This is the opposite of multiple inheritance.
Syntax:-
Class P
{
}
Class Q extends P
{
}
Class R extends P
{
}
Class S extends P
{
}
Hierarchical inheritance Program:
Hybrid inheritance in java:
When we mix any two types of inheritance in our program, it is called hybrid inheritance. This inheritance is a combination of more than one inheritance. That is, it is made up of two or more inheritances.
Hybrid inheritance is a collection of multiple and multi-level inheritance. It has more than one base class and more than one derive class
Syntax :-
Class A
{
}
Class B extends A
{
}
Class C
{
}
Class D extends B , extends C
{
}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining