What is Inheritance ?
Inheritance in Java, is a technique in which a class (is called child class) holds / acquires all the properties and method of other class (is called parent/base class).
This is one of the property of OOPS ( Object Oriented Programming Structure).
The main aim to use the Inheritance in Java, is reusability
For Example:
Using inheritance, we can create a class A and this class use the properties of class B and we can also add some method in class A. So both class methods can be accessed through the object of class A. Here class B is reusing by class A.
Syntax of Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Four main terms used in the Syntax
- Class
- Sub-class
- Super-class
- extends
- Class - A class is a collection of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub-class - Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super-class – Super-class is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Extends - The extends keyword indicates that you are making a new class that derives from an existing class.
Types of Inheritance in Java
There are three types of Inheritance in Java using Class.
- Single Level Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Other Two types of Inheritance,
- Multiple Inheritance
- Hybrid Inheritance
Only create by Interface in Java. But not created by class.
Single Level Inheritance in Java
a class A inherits another class B, it is known as a single inheritance.
Example Single Level Inheritance
class A{
void displayA(){System.out.println(“Class A...");}
}
class B extends A{
void displayB(){System.out.println(“Class B...");}
}
class singleinheritance{
public static void main(String args[]){
B b=new B();
b.displayB();
b.displayA();
}}
Output:
Class B…
Class A…
Multi-Level Inheritance in Java
When a class A inherit another class B and class B another class C and so on or like a chain of inheritance, it is known as multilevel inheritance.
class A{
void displayA(){System.out.println(“Class A...");}
}
class B extends A{
void displayB(){System.out.println(“Class B...");}
}
class C extends B{
void displayC(){System.out.println(“Class C...");}
}
class multilevelinheritance{
public static void main(String args[]){
C b=new C();
b.displayB();
b.displayA();
b.displayC();
}}
Output:
Class B…
Class A…
Class C…
Hierarchical Inheritance in Java
When two or more classes inherits a single class, it is known as hierarchical inheritance.
class A{
void displayA(){System.out.println(“Class A...");}
}
class B extends A{
void displayB(){System.out.println(“Class B...");}
}
class C extends A{
void displayC(){System.out.println(“Class C...");}
}
class multilevelinheritance{
public static void main(String args[]){
C b=new C();
b.displayA();
b.displayC();
}}
Output:
Class A…
Class C…
Here, Class C only use the method of class A, and class B also use the method of Class A. B don’t use the method of Class C and C don’t use the method of Class B.
Multiple Inheritance in Java
When a class A inherit two or more classes ( B, C and so on ) is called Multiple Inheritance.
Hybrid Inheritance in Java
When more than one type of inheritance technique used under a inheritance technique is called Hybrid Inheritance.
Multiple and Hybrid Inheritance is done by only using the Interface in Java.