0% found this document useful (0 votes)
4 views

Inheritance in Java Oop

Uploaded by

zunairam841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Inheritance in Java Oop

Uploaded by

zunairam841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Inheritance in Java

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.

Why use inheritance in java


For Method Overriding (so runtime polymorphism can be achieved).

For Code Reusability.

Terms used in Inheritance


o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.

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 syntax of Java Inheritance

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.

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

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 {

public void methodX() {

System.out.println("Class X method");

class Y extends X {

public void methodY() {

System.out.println("Class Y method");

class Z extends Y {

public void methodZ() {

System.out.println("Class Z method");

public static void main(String[] args) {

Z obj = new Z();

obj.methodX(); // calling grandparent class method

obj.methodY(); // calling parent class method

obj.methodZ(); // calling local method

}
Hierarchical Inheritance in Java
The type of inheritance where many subclasses inherit from one single class is known as Hierarchical
Inheritance.

Hierarchical Inheritance a combination of more than one type of 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.

It also provides the code reusability.

The hybrid inheritance can be achieved by using the following combinations:

o Single and Multiple Inheritance (not supported but can be achieved through interface)

o Multilevel and Hierarchical Inheritance

o Hierarchical and Single Inheritance

o Multiple and Multilevel Inheritance

Q) Why multiple inheritance is not supported in java?


To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

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.

Types of Java Polymorphism


1. Compile Time Polymorphism

❖ Static Polymorphism
❖ Method Overloading

Note: It is handled by the Compiler

2. Run Time Polymorphism

❖ Dynamic Polymorphism
❖ Method Overriding

Note: Handled by JVM (Java virtual Machine)

Compile-time polymorphism is also known as static polymorphism and it is implemented by method


overloading.

Method Overriding

Same class name Same method name

Method
overloading

Different Paramenters Note: The Method


Different data type, Sequance or number of
parameters return type cant effect
Example : Polymorphism using 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");

// call method with a single argument


d1.display('#');
}
}
Method Overriding

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

For Example: Polymorphism using method overriding

class Language {

public void displayInfo() {

System.out.println("Common English Language");

class Java extends Language {

@Override

public void displayInfo() {

System.out.println("Java Programming Language");

class Main {

public static void main(String[] args) {


// create an object of Java class

Java j1 = new Java();

j1.displayInfo();

// create an object of Language class

Language l1 = new Language();

l1.displayInfo();

You might also like