Interview Question

What is inheritance in Java?

A class inherits from one direct superclass and can specialize inherited behavior.

💡 Concept ✅ Quick Revision ☕ Java

Answer

Class inheritance lets a subclass derive members and behavior from one direct superclass. • The extends clause names the superclass. • Constructors are not inherited. • Private members are not directly accessible in the subclass, though the superclass portion still contains its state.

Example

Code
class Animal {
    String speak() { return "sound"; }
}
class Dog extends Animal {}
System.out.println(new Dog().speak());
Output
sound

Quick Revision

A class inherits from one direct superclass and can specialize inherited behavior.