Loading
Java Inheritance-interview

Q.What is inheritance in Java?

Inheritance in Java is a mechanism where one class (child/derived class) can acquire the properties and behaviors (fields and methods) of another class (parent/superclass).
It promotes code reusability and establishes an is-a relationship between classes.

Example


class Vehicle {

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

}

class Car extends Vehicle {

    void drive() { System.out.println("Car is driving"); }

}



Q. Can constructors be inherited in Java?

No, constructors are not inherited.
However, a child class can call the parent constructor using super().



Q. Can we prevent inheritance in Java?

Yes.

  • By declaring a class as final, it cannot be extended.
  • By declaring methods as final, they cannot be overridden.


Q. Can private members of a class be inherited?
Yes, but they are not accessible directly in the subclass.
They are inherited but remain hidden. A child can access them only via getters/setters or protected methods in the parent.


Q. What happens if a subclass defines a method with the same name but different parameters?

That is method overloading, not overriding.
Overloading happens at compile-time, overriding at runtime.