What is inheritance in Java?

Inheritance is one of the core principles of Object-Oriented Programming (OOP) that allows a class to acquire the properties and behaviors of another class. It promotes code reusability and helps establish an "is-a" relationship between classes.

Key Points: • Inheritance enables a child class to reuse fields and methods of a parent class. • It reduces code duplication and improves maintainability. • Java uses the extends keyword to implement class inheritance. • A subclass can add new features or override inherited methods to provide specialized behavior. • Java supports single inheritance for classes and multiple inheritance through interfaces.

Example: A Vehicle class can contain common properties such as speed and methods like start(). A Car class can inherit these features from Vehicle and also define its own specific functionality.

Code Example:

class Vehicle {
    void start() {
        System.out.println("Vehicle Started");
    }
}

class Car extends Vehicle {
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.start();
    }
}

Interview Tip: A concise interview answer is:

"Inheritance is an OOP feature that allows one class to inherit the properties and methods of another class using the extends keyword. It promotes code reusability, establishes an is-a relationship, and helps create a hierarchical class structure."