Java Tutorial


OOPS concept


Collection

Java Inheritance

Inheritance is the process, where one class inherits the properties of the another class.

The class which inherits the properties is known as derived class or sub-class or child class and the class from which the properties are inherited is known as base class or super class or parent class.

It represents 'IS-A' relationship.

Why Inheritance?

Inheritance is used for code reusability.

Syntax

class Parent{
...
...
...
}

class Child extends Parent{
...
...
...
}

The extends keyword is used to inherit the class properties.

Let's take an example and understand it in detail.

Example

class Animal{
  void speed(){}
  void sound(){}
}

class Dog extends Animal{
}

class Tiger extends Animal{
}

In the above example, you can see that Dog class and Tiger class is extending Animal class. Here you can also see the 'IS-A' relationship.

Dog IS-A Animal

Tiger IS-A Animal

Types of Inheritance

Following are the types of inheritance.

Single Inheritance

Single Inheritance

Multilevel Inheritance

Multilevel Inheritance

Multiple Inheritance

Multiple Inheritance

Hybrid Inheritance

Hybrid Inheritance

Hierarchical Inheritance

Heirarichal Inheritance
Method Overriding in Java

Advertisment

Share


Advertisement