Inheritance is a powerful object-oriented programming feature offered by Java.
It is one of the four main pillars (core concepts) of OOP: Encapsulation, Inheritance, Polymorphism, and Abstraction.
The technique of creating a new class by using an existing class functionality is called inheritance in Java.
The existing class is called parent class (a more general class) and the new class is called child class (a more specialized class).
The child class inherits data and behavior from the parent class.
What is Is-A Relationship in Java?
IS-A relationship in Java represents inheritance. It is implemented in Java using:
the extends keyword for class inheritance, and
(the implements keyword for interface implementation).
Object Class
_All the classes in Java extends the class java.lang.object by default. _This is one of the best example of an IS-A relationship in Java.
This means Object is the root class of all the classes in Java.
By default, every class is the subclass of Object either directly or indirectly.
Therefore, every class inherits methods of the Object class. Hence, we can say that Object class is the superclass of every class.
Note that a class always extends another class.
TYPES OF INHERITANCE:
Single Inheritance: In single inheritance, a single subclass inherits from a single superclass. This is the simplest form of inheritance and establishes a straightforward "is-a" relationship,
Multilevel Inheritance: This involves a chain of inheritance where a class inherits from a base class, and that derived class then acts as a base class for another class
Hierarchical Inheritance: This type occurs when multiple subclasses inherit from a single superclass.
How is Inheritance Implemented/Achieved in Java?
Inheritance in Java can be implemented or achieved by using two keywords:
Unsupported Inheritance:
Multiple Inheritance.
Java does not Support Multiple Inheritance through Classes
If a subclass inherits from more than one superclass, it receives all the accessible methods and member variables of the superclass. If we use multiple inheritance in Java, it can lead ambiguity, complexity, and confusion for the programmer.
Letβs take a simple scenario to understand why Java does not support multiple inheritance through classes.
Consider a scenario as shown in the below figure. In this scenario, there are three classes A, B, and C. Class C extends two parent classes such as class A and class B. Assume that class A and class B have the same method msg() (say) with different implementations.
Multiple inheritance is not supported by Java because of** Diamond Problem**
According to inheritance concepts, both methods will inherit to class C. If we construct an object of class C and call the msg() method using the child class object reference, which msg() method will be invoked? (i.e. which copy of msg() method will be available in class C?)
In this situation, Java compiler will not be able to determine which of the methods in the parent class is to be called. Hence, it will create ambiguity and confusion in calling the method msg() from A or B class.
Since the compile-time error is better than a runtime error, Java will give a compile-time error if we extend more than one class. So, whether you have the same or different method signature still you will get compile-time error.
Note:We can achieve the functionality of multiple inheritance in Java by using interface.
Important Rules of Java Inheritance
There are the following rules of using inheritance in Java:
You cannot assign a superclass to the subclass.
You cannot extend the final class because a class declared with the final keyword cannot be inherited.
A class cannot extend itself.
A class can extend only one class.
You cannot extend a class that has only a private constructor. However, if the class has a private constructor and at least one public or protected constructor, it can be extended.If you assign a subclass object to a superclass reference, it is called upcasting in Java.
Constructor, Static initialization block (SIB), and Instance initialization block (IIB) of the superclass are not inherited by the subclass. However, they are executed while creating an object of the subclass.
A static method of a superclass is accessible from a subclass, but it is not inherited in the same way as instance methods. Static methods are hidden, not overridden.



Top comments (0)