Inheritance in Java

Inheritance in Java

The main goal of inheritance is reusability. Inheritance allows one class (subclass/child) to acquire methods and variables of another class (superclass/parent).

Example with Selenium

Syntax

Types of Inheritance in Java

1. Single Inheritance

2. Multilevel Inheritance

3. Multiple Inheritance (Not Supported in Classes)

Java does not allow a class to extend more than one class. This prevents the diamond problem. Instead, multiple inheritance is achieved using interfaces.

Special Note: Object Class

In Java, every class implicitly extends the Object class. That’s why methods like toString(), hashCode(), and equals() are available in all classes.

Selenium Example with Inheritance