Last Updated: 26 November, 2023
Inheritance is one of the strong features of object-oriented programming (OOP) that allows a class to access (or inherit) properties, such as variables and methods of another class. This technique promotes code reusability and facilitates the creation of hierarchical class structures that represent real-world relationships.
The extends keyword is used to implement the concept of inheritance in Java.
A class can extend only one class, Java does not allow extending multiple classes.
In inheritance, a child class inherits the features of its parent class, but the parent class never inherits the features of its child class.
A child class can access only the non-private properties of its parent class.
Inheritance Syntax in Java
The extends keyword is used in places where one class has to inherit the properties (variables and methods) of another class. In the case of interfaces, we use the extends keyword if one interface wants to inherit the properties of another interface.
For example,
class A {}
class B extends A {}
interface Y {}
interface X extends Y {}
Class: A class is a user-defined data type in Java from which objects are created. It represents the set of properties, methods, etc. that are common to all objects of one type. A single Java class can have one or more objects.
Super Class: A class from which the properties are inherited is called the Super Class, Parent Class, or Base Class in Java.
Sub Class: A class that inherits the properties of another class is called a subclass, child class, extended class, or derived class in Java.
Reusability: Inheritance promotes code reusability in Java, which means already developed Java classes and their members (variables and methods) can be used in other classes. This will save our development and testing time and efforts.
Inheritance provides a couple of benefits. Let's understand how inheritance provides advantages in the Java programming language.
✅ | With the help of inheritance, we can reuse existing class properties, such as variables and methods, in another class. |
✅ | Inheritance saves our development, testing, bug-fixing, and maintenance time, effort, and cost as well. |
✅ | Inheritance provides a clear model structure that is easy to understand and implement. |
✅ | Inheritance can be used for method overriding in Java. |
Along with the benefits, there are some points where we have to pay the costs of using inheritance in Java.
✅ | If we modify or remove the properties of the parent class, that will directly affect the behavior of the child class too. |
✅ | The subclass becomes dependent on the parent class's implementation. |
✅ | Inheritance bit impacts the execution speed because inheritance execution takes time and effort. |
✅ | Inheritance also promotes a tight coupling. Both the parent class and child class get tightly bound to each other, and a developer cannot use these classes independently of each other. |
✅ | The overuse of inheritance makes Java applications more complex. |
Inheritance establishes an "IS-A" relationship between classes, where one class (subclass) inherits the features of another class (superclass). This mechanism promotes code reusability, enhances maintainability, and encourages modularity in software design.
In Java, we use inheritance only if there exists an "IS-A" relationship between two classes.
For example,
A car is a vehicle
Mango is a fruit
Lion is an animal
Here, a car can inherit from a vehicle, a mango can inherit from fruit, a lion can inherit from an animal, and so on.
There are five types of inheritances, and they are as follows.
Note: Multiple inheritance and hybrid inheritance are only supported through the interface in Java; we cannot achieve them through Java classes.
Let's go over each type of inheritance in Java one by one, using definitions and examples.
In single inheritance, a single class (known as the child class) inherits the properties (variables and methods) of another single class (known as the parent class). Only two classes are required to implement single inheritance in Java.
Let's understand single inheritance through the diagram and example below.
Example 1 : A sample Java program of Single Inheritance
Output
Father's Money : 20 Lakhs
Son's Money : 10 Lakhs
It's a Father's House
It's a Son's House
In multilevel inheritance, a child class will inherit a parent class, and the child class also acts as the parent class for another class.
Let's understand multilevel inheritance through the diagram and example below.
Example 2 : A sample Java program of Multilevel Inheritance
Output
Father's Money : 20 Lakhs
Son's Money : 10 Lakhs
Grand Son's Money : 5 Lakhs
It's a Father's House
It's a Son's House
It's a Gand Son's House
In hierarchical inheritance, one superclass has multiple subclasses, each inheriting the superclass's same properties (variables and methods).
Let's understand hierarchical inheritance through the diagram and example below.
Example 3 : A sample Java program of Hierarchical Inheritance
Output
Father's Money : 20 Lakhs
First Son's Money : 10 Lakhs
It's a Father's House
It's a First Son's House
Father's Money : 20 Lakhs
Second Son's Money : 8 Lakhs
It's a Father's House
It's a Second Son's House
Father's Money : 20 Lakhs
Third Son's Money : 12 Lakhs
It's a Father's House
It's a Third Son's House
In multiple inheritance, a single child class extends from multiple parent classes, which means one child class will have more than one parent class, but this implementation is not supported by Java; we can achieve this only using interfaces.
Let's understand multiple inheritance through the diagram below.
Hybrid inheritance is a combination of two or more types of inheritance in Java. Same as Multiple Inheritance, Hybrid Inheritance is also not supported by using Java classes; however, we can achieve this type of implementation using the Java Interface.
Let's understand hybrid inheritance through the diagram below.
Using Interface, we can implement the same as given in the below diagram.
In the above diagram, we are using the extends and implements keywords.
We use the extends keyword for the same data type, like one class can extend another class, same as one interface can extend another interface, but in Java, one class can extend only one class, whereas one interface can extend one or more interfaces.
implements keyword use with different data types, like one class can implement another interface. Note that one class can implement one or more than one interface, but an interface can never implement any class in Java.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why Java does not support Multiple Inheritance?
Ans. Multiple Inheritance means one class can extend more than one class, but Java does allow it. because of the following reasons:
Why Inheritance is one of the strong feature of Java?
Ans. The following points are mentioned below, which make inheritance one of the strongest features of Java:
Can a class extend itself in Java?
No, a class can not extend itself in Java.
Can we assign superclass to subclass?
No.
What is IS-A relationship in Java?
Ans. In Java programming, if a class extends another class using the extends keyword, it is called an IS-A relationship and it only happens in inheritance. A minimum of two classes are required to establish an IS-A relationship in Java.
For example, if a class Mango inherits another class Fruits, then we can say that Mango is having an is-a relationship with Fruits, which implies Mango is a Fruit.
What is order of calling constructors in inheritance?
Ans. In inheritance, constructors are called from the base class to derived class that means first base class constructors will be called then derived class constructors.
For example,
class A { A() { System.out.println("A class constructor called."); } } class B extends A { B() { System.out.println("B class constructor called."); } } class C extends B { C() { System.out.println("C class constructor called."); } } public class ConstructorsCallinginInheritance { public static void main(String[] args) { C obj = new C(); } }
Output
A class constructor called.
B class constructor called.
C class constructor called.
How we can restrict a member of a class from inheriting its subclass?
We can restrict members of a class by declaring them private because the private members of a superclass are not available to the subclass directly. They are only available in their own class.
Is it possible to access child class members using a parent class object in inheritance?
No, we can only access parent class members using the parent class object, not child class members.
If the property name of the parent and child classes is the same, which will be called using the child class object?
If we call the same name property by a child class object, then only the child class property will be called, not the parent class property.