Inheritance in Java.

Inheritance is a process in which one object acquires all the properties & behavior of a parent object. When you inherit from an existing class, you can reuse methods & fields of the parent class. Inheritance represent IS-A relationship which is also known as a parent-child relationship.

superclass (parent) - the class being inherited from.

subclass (child) - the class that inherit from another class.

Use of Inheritance in java.

  1. For method overloading (to achieve polymorphism at runtime).

  2. For code Reusability (to avoid code repeatedly).

To inherit from a class extends keyword is used.

Syntax.

Class Subclass-name extends Superclass-name {

// methods

}

Types of Inheritance

  1. Single

  2. Multilevel

  3. Hierarchical

  4. Multiple (not used in Java)

  5. Hybrid.