Inheritance
Inheritance is one of the key features of OOP that allows us to create a new class from an
existing class. Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another. With the use of inheritance the information is
made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
parent class).
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Why use inheritance in java
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
Terms used in Inheritance
o Class: A class is a group of objects which have common properties. It is a template
or blueprint from which objects are created.
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.
o Reusability: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class.
Er. Rahul Mishra
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Syntax
Class Super
{
//methods and filed
}
//use of extends keyword
//to perform inheritance
class Sub extends Super
{
//methods and field of Animals
//methods and fields of Dog
}
In the above example, the Sub class is created by inheriting the methods and fields from
the Super class. Here, Sub is the subclass and Super is the superclass.
Er. Rahul Mishra
Example 1: Java Inheritance
class Animal
{
String name;
public void eat()
{
System.out.println("I can eat");
}
}
class Dog extends Animal
{
public void display()
{
System.out.println("My name is " + name);
}
}
class Main
{
public static void main(String[] args)
{
Dog labrador = new Dog();
labrador.name = "Tiger";
labrador.display();
labrador.eat();
}
}
Er. Rahul Mishra
Types of inheritance
There are five types of inheritance.
1. Single Inheritance
In single inheritance, a single subclass extends from a single superclass. For example,
Syntax:
Class A
//methods and field of class A
//methods and fields of class A
Class B extends A
//methods and field of class B
//methods and fields of class B
}
Er. Rahul Mishra
2. Multilevel Inheritance
In multilevel inheritance, a subclass extends from a superclass and then the same subclass
acts as a superclass for another class. For example,
Syntax:
Class A
//methods and field of class A
//methods and fields of class A
}
Class B extends A
//methods and field of class B
//methods and fields of class B
}
Class C extends B
//methods and field of class C
//methods and fields of class C
}
Er. Rahul Mishra
3. Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses extend from a single superclass. For
example,
Syntax:
Class A
//methods and field of class A
//methods and fields of class A
}
Class B extends A
//methods and field of class B
//methods and fields of class B
}
Class C extends A
//methods and field of class C
//methods and fields of class C
}
Er. Rahul Mishra
4. Multiple Inheritance
In multiple inheritance, a single subclass extends from multiple superclasses. For
example,
Note: Java doesn’t support multiple inheritances. However, we can achieve multiple
inheritances using interface.
Sysntax:
interface A
//methods and field of class A
//methods and fields of class A
}
interface B
//methods and field of class B
//methods and fields of class B
}
Class C implements A, B
//methods and field of class C
//methods and fields of class C
}
Er. Rahul Mishra
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. For example,
Here, we have combined hierarchical and multiple inheritance to form a hybrid
inheritance.
Sysntax:
interface A
//methods and field of class A
//methods and fields of class A
}
interface B extends A
//methods and field of class B
//methods and fields of class B
}
Er. Rahul Mishra
interface C extends A
//methods and field of class C
//methods and fields of class C
}
Class D implements B, C
//methods and field of class B
//methods and fields of class B
}
Note:-
A Class can extends another class
A class can implements an interface
An interface can extends another interface
Er. Rahul Mishra