Interview Q&A · Java · 4 questions in this topic
Inheritance and Polymorphism Explained — Java Interview Q&A
Encapsulation, inheritance and polymorphism in depth, with the analogies and mechanisms interviews expect. Part of OOP Concepts · 294 questions in the Java bank.
What is encapsulation?
Answer
Encapsulation is the wrapping of code and data into a single unit, the class, with only the safe parts exposed to the outside world. Java implements it through access control, typically private fields reached through public getters, setters or behaviour methods that can validate use. The television remote is the classic analogy: viewers get a few buttons while thousands of components stay hidden, so the internals can change without retraining every user.
Key points
- 1Controlled exposure: public methods define the supported operations on hidden state.
- 2Information hiding: callers cannot depend on representation details that may change.
- 3Validation: methods can reject bad input instead of letting fields be corrupted directly.
💡 Pair the definition with the remote-control analogy and one access-control example for full marks.
What is inheritance?
Answer
Inheritance lets a new class acquire the members of an existing class and then extend or specialise them, modelling an IS-A relationship such as a car IS-A vehicle. The existing type is called the base, parent or super class, and the new type is called the derived, child or sub class. In Java the link is declared with extends, and every class ultimately descends from java.lang.Object.
Key points
- 1Declared with extends: the subclass inherits accessible fields and methods.
- 2Creates an IS-A link: a Car can be used wherever a Vehicle is expected.
- 3Single class inheritance: a class extends at most one superclass, plus any number of interfaces.
Example
class Vehicle { void start() { System.out.println("Starting"); } } class Car extends Vehicle { void openTrunk() { System.out.println("Opening trunk"); } }
Why is inheritance important?
Answer
The headline benefit is reuse: common state and behaviour live once in a parent instead of being duplicated across children. It also enables extension without surgery, since new subclasses can add behaviour while existing, tested code stays untouched. Library evolution shows the pattern: each new release reuses the proven core and layers fresh features on top rather than rewriting everything.
Key points
- 1Reusability: shared logic is written once and inherited everywhere it applies.
- 2Extendability: new functionality arrives through subclasses, leaving stable code alone.
- 3Framework design: platforms ship base types that applications specialise for their own needs.
What is polymorphism?
Answer
Polymorphism means many forms: one call site can trigger different behaviour depending on the actual object type involved. In Java the classic mechanism is runtime dispatch, where an overridden method or interface implementation is selected by the real object rather than the reference type. Method overloading is the compile-time cousin, resolved from the static signature instead of the runtime type.
Key points
- 1Runtime polymorphism: overriding plus interfaces, resolved by the actual object type.
- 2Compile-time polymorphism: overloading, resolved from parameter types at compile time.
- 3Benefit: uniform client code works with whole families of implementations.
Example
class Animal { void speak() { System.out.println("Sound"); } } class Dog extends Animal { void speak() { System.out.println("Bark"); } } Animal a = new Dog(); a.speak(); // Bark: the object type decides
💡 Stress the contrast: the reference type chooses the signature while the object type chooses the behaviour.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission