Inheritance in Java Oop
Inheritance in Java Oop
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover,
you can add new methods and fields in your current class also.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is
also called a base class or a parent class.
The extends keyword indicates that you are making a new class that derives from an existing class. The
meaning of "extends" is to increase the functionality.
As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The
relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type
of Employee.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn
about interfaces later.
Note: Multiple inheritance is not supported in Java through class.
When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Single Inheritance
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog
class inherits the Animal class, so there is the single inheritance.
Multilevel Inheritance
The multi-level inheritance includes the involvement of at least two or more than two classes. One class
inherits the features from a parent class and the newly created sub-class becomes the base class for
another new class.
As the name suggests, in the multi-level inheritance the involvement of multiple base classes is there. In
the multilevel inheritance in java, the inherited features are also from the multiple base classes as the
newly derived class from the parent class becomes the base class for another newly derived class.
class X {
System.out.println("Class X method");
class Y extends X {
System.out.println("Class Y method");
class Z extends Y {
System.out.println("Class Z method");
}
Hierarchical Inheritance in Java
The type of inheritance where many subclasses inherit from one single class is known as Hierarchical
Inheritance.
It is different from the multilevel inheritance, as the multiple classes are being derived from one
superclass. These newly derived classes inherit the features, methods, etc, from this one superclass. This
process facilitates the reusability of a code and dynamic polymorphism (method overriding).
we can observe that the three classes Class B, Class C, and Class D are inherited from the single Class A.
All the child classes have the same parent class in hierarchical inheritance.
Hybrid inheritance
In Java, inheritance is the most important OOPs concept that allows to inherit the properties of a class
into another class. in general, it defines Is-A relationship. By using the inheritance feature, we can derive
a new class from an existing one
In general, the meaning of hybrid (mixture) is made of more than one thing. In Java, the hybrid
inheritance is the composition of two or more types of inheritance.
o Single and Multiple Inheritance (not supported but can be achieved through interface)
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B
classes have the same method and you call it from child class object, there will be ambiguity to call the
method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2
classes. So whether you have same method or different, there will be compile time error.
For example, there are four classes say A, B, C, and D. Assume that the class "A" and "B" extends the
class "C". Also, another class, "D," extends the class "A". Here, the class "A" is a parent class for child
class "D" and is also a child class for parent class "C".
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.
We can perform polymorphism in java by method overloading and method overriding.
❖ Static Polymorphism
❖ Method Overloading
❖ Dynamic Polymorphism
❖ Method Overriding
Method Overriding
Method
overloading
class Pattern {
// method without parameter
public void display() {
for (int i = 0; i < 10; i++) {
System.out.print("*");
}
}
// method with single parameter
public void display(char symbol) {
for (int i = 0; i < 10; i++) {
System.out.print(symbol);
}
}
}
class Main {
public static void main(String[] args) {
Pattern d1 = new Pattern();
// call method without any argument
d1.display();
System.out.println("\n");
During inheritance in Java, if the same method is present in both the superclass and the subclass. Then,
the method in the subclass overrides the same method in the superclass. This is called method
overriding.
In this case, the same method will perform one operation in the superclass and another operation in the
subclass.
Same
method
name
Method
overriding
Different Same
Class(inherited
) parameters
class Language {
@Override
class Main {
j1.displayInfo();
l1.displayInfo();