In this article we learn the what is inheritance in java, Types of inheritance and what is super, this, extends & implements keywords in java.
What is Inheritance in Java?
Inheritance in java can be defined as the process where one class acquires the properties (methods and fields) of another.
Important Terms in Java Inheritance
a) Class: Class is a user-defined datatype in Java that is basically a group of objects. It is a blueprint or template from which we create objects.
b) Super Class: The class whose features and functionalities are being inherited or used is known as the superclass or a base class or a parent class.
c) Sub Class: The class that inherits the properties and features from another class is known as a subclass or a derived class or extended class or child class. The subclass can add its own features and functions in addition to the fields and methods of its superclass or the parent class.
Real Time Examples Inheritance in Java: A child inherits all the properties of his parents.
Types of Inheritance in Java
1) Single Inheritance in Java
2) Multiple Inheritance in Java
3) Multi-Level Inheritance in Java
4) Hierarchical Inheritance in Java
1) Single Inheritance in Java
In single inheritance, there is a single child class that inherits properties from one parent class.
Single Inheritance in Java |
The above diagram shows that class B extends only one class which is A.
Class A is a Parent class (super Class) of B and B would be a child class of A.
Syntax:
class A
{
//methods and fields
}
Class B extends A
{
//methods and fields
}
2) Multiple Inheritance in Java
Multiple Inheritance is one of the inheritances in Java types where one class extending more than one class. Java does not support multiple inheritance using only classes. It is achieved by using one class and multiple /Single interfaces.
As per below diagram, Class C extends Class A and Class B both.
Multiple Inheritance in Java |
3) Multi-Level Inheritance in Java
When multiple classes are involved and their parent-child relation is formed in a chained way then such formation is known as multi-level inheritance.
In multi-level inheritance, a parent a class has a maximum of one direct child class only.
In multi-level inheritance, the inheritance linkage is formed in a linear way and minimum 3 classes are involved.
Multi-Level Inheritance in Java |
In the above diagram, class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So, in this case class C is implicitly inheriting the properties and methods of class A along with class B that’s what is called multilevel inheritance.
4) Hierarchical Inheritance in Java
When there are 2 or more classes involved and there is a common parent for 2 or more child classes, such relation is hierarchical inheritance.
Hierarchical Inheritance in Java |
In above diagram, class B, C and D extends a same class A.
Hierarchical Inheritance in Java Example
class A {
}
class B extends A {
}
class C extends A {
}
Super Keyword in Java
The super keyword in Java is used to refer to the object of the parent or superclass. It is used to call the immediate parent class methods.
By default, hidden in all constructors.
It should be first statement of constructor.
It is used to call parent Constructor
We can call parent’s default or parameterized constructor.
To call parent’s class, parameterized constructor, we can pass parameters in super(…).
this Keyword In Java
The this keyword in Java is used to refer to the current object of the class inside the method or a constructor.
Used to call same class constructors.
It should be first statement of constructor
Can we use both this () and super () together?
Both this () and super () cannot be used together in constructor.
this () is used to call default constructor of same class.it should be first statement inside constructor. Super () is used to call default constructor of parent class.it should be first statement inside constructor.
We use either super () or this () as first statement inside constructor and not both.
Extends Keyword in Java
The extends keyword extends a class and is an indicator that a class is being inherited by another class. When we say class B extends a class A, it means that class B is inheriting the properties (methods, attributes) from class A. Here, class A is the superclass or parent class, and class B is the subclass or child class.
implements Keyword In Java
The implements keyword in Java is used by a class to implement an interface. Implementing an interface allows a class to use the methods defined in an interface.