Java Inheritance Quiz | 25 OOP MCQs for Interviews & Exams

Yogi Siddeswara 0
STUDYECRAT Java : OOP - INEHRITANCE
60s
Java : OOP - INEHRITANCE

Prove your skills in this interactive quiz

💻

Live Code

Run snippets directly

⏱️

Timed

60s per question

🏆

Scored

Earn 3D badges

Java Inheritance: Key Interview Points

1. Dynamic Method Dispatch

class Animal {
  void sound() { System.out.print("Generic Sound"); }
}
class Dog extends Animal {
  @Override void sound() { System.out.print("Bark"); }
}

Animal myPet = new Dog();
myPet.sound(); // Output: Bark
// Runtime polymorphism in action
  • Tip: Use @Override annotation to prevent accidental method signature mismatches
  • Real Use: Plugin architectures where child classes provide specific implementations

2. Field Shadowing

class Parent {
  int value = 10;
}
class Child extends Parent {
  int value = 20; // Shadows parent's field
}

Parent obj = new Child();
System.out.print(obj.value); // Output: 10
// Variables don't participate in polymorphism
  • Tip: Avoid field shadowing - use different names or protected access
  • Real Use: Configuration classes where default values might be overridden

3. Constructor Inheritance

class Vehicle {
  Vehicle() { System.out.print("Vehicle "); }
}
class Car extends Vehicle {
  Car() { System.out.print("Car"); }
}

new Car(); // Output: Vehicle Car
// Parent constructor always executes first
  • Tip: Use super(params) to call specific parent constructors
  • Real Use: Initializing common resources in base class constructors

4. Accessing Parent Members

class Phone {
  void call() { System.out.print("Calling"); }
}
class SmartPhone extends Phone {
  @Override void call() {
    super.call(); // Parent implementation
    System.out.print(" with Video");
  }
}

new SmartPhone().call(); // Output: Calling with Video
// Extending functionality while reusing parent logic
  • Tip: super() must be the first statement in constructor if used
  • Real Use: Middleware implementations that wrap core functionality

5. Preventing Inheritance

final class SecurityKey { /* Cannot be extended */ }

class PaymentProcessor {
  final void validate() { /* Critical security logic */ }
}
class PayPal extends PaymentProcessor {
  // Compile Error: Cannot override final method
}
// Final restricts extension for security/design
  • Tip: Mark utility classes and security methods as final
  • Real Use: Immutable classes like String in Java standard library

💡 Pro Interview Tip

When asked about inheritance, always mention the "is-a" relationship principle. For example: "A Dog is-a Animal" justifies inheritance, while "Car has-a Engine" suggests composition instead.


Java Operators Quiz: 25 MCQ Questions with Answers

Post a Comment

0 Comments