Inheritance allows a child class to acquire properties and methods from a parent class.

It is used for code reuse and for modeling an is-a relationship.

Example

class BaseJob {
    void recordStart() { System.out.println("Job started"); }
}

class DataExportJob extends BaseJob {
    void writeCsv() { System.out.println("Writing CSV"); }
}

DataExportJob inherits recordStart() from BaseJob.

Why Inheritance Is Used

Types of Inheritance

Single

BaseJob -> DataExportJob

Multilevel

BaseJob -> DataExportJob -> ScheduledDataExportJob

Hierarchical

BaseJob -> DataExportJob
BaseJob -> CleanupJob

Multiple

Java does not support multiple inheritance with classes because it can create ambiguity. Java supports multiple inheritance of capability through interfaces.

What Is Inherited?

A child inherits non-private fields and methods.

Constructors are not inherited.

Is-A Rule

Use inheritance when the child truly is a parent type.

Interview Notes