Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
A child inherits features from parents — eye color, height, surname. In Java, a child class inherits fields and methods from its parent class. No need to rewrite what already exists.
Real World Analogy
All vehicles have wheels, engine, fuel. A Car IS a vehicle. A Truck IS a vehicle. Instead of writing wheels/engine for every vehicle type separately — define it ONCE in Vehicle, inherit everywhere.
What is Inheritance?
A mechanism where one class (child/subclass) acquires the properties and methods of another class (parent/superclass).
java
QUBITS OF DPK
Types of Inheritance
1. Single Inheritance
javascript
QUBITS OF DPK
2. Multilevel Inheritance
javascript
QUBITS OF DPK
java
QUBITS OF DPK
3. Hierarchical Inheritance
javascript
QUBITS OF DPK
4. Multiple Inheritance — NOT supported directly
java
QUBITS OF DPK
Why? Diamond Problem:
javascript
QUBITS OF DPK
Java's Solution: Use Interfaces for multiple inheritance.
super Keyword
java
QUBITS OF DPK
super() — Call Parent Constructor
java
QUBITS OF DPK
Method Overriding
Child class provides its own implementation of a method inherited from parent.
java
QUBITS OF DPK
Overriding Rules
javascript
QUBITS OF DPK
Overriding vs Overloading
Packages & Access Modifiers
Packages
java
QUBITS OF DPK
Access Modifiers
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Inheritance allows a child class to acquire fields and methods from a parent class via the 'extends' keyword, promoting code reuse. Java supports single, multilevel, and hierarchical inheritance. Multiple inheritance via classes is not supported due to the diamond problem — interfaces solve this. 'super' accesses parent members and super() calls parent constructor (must be first line). Method overriding lets child redefine parent methods — resolved at runtime (dynamic polymorphism). Always use @Override annotation to catch typos. Access modifiers control visibility: private < default < protected < public."
Interview Questions & MAANG-Level Answers
Q1. What is inheritance and what problem does it solve?
Inheritance allows a child class to acquire fields and methods from a parent class via extends. Problem it solves: code reuse and avoiding duplication. Without inheritance: every Vehicle subtype (Car, Truck, Bike) would duplicate start(), stop(), fuelLevel etc. With inheritance: define once in Vehicle, all subtypes inherit. Also enables polymorphism — Vehicle v = new Car() — write generic code that works for all subtypes.
Q2. Why doesn't Java support multiple inheritance with classes?
Because of the diamond problem. If class C extends both A and B, and both A and B override method fly() from a common parent, then C inherits two conflicting implementations of fly() — ambiguous. Java resolves this by only allowing single class inheritance. Multiple inheritance is supported through interfaces where Java 8+ requires you to explicitly override conflicting default methods.
Q3. What is the diamond problem?
javascript
QUBITS OF DPK
D inheriting from both B and C creates ambiguity about which fly() implementation D gets. Java prevents this by disallowing class D extends B, C. With interfaces: if B and C are interfaces with default fly(), class D must explicitly override fly() and can use B.super.fly() to call a specific one.
Q4. What is the difference between method overloading and overriding?
Overloading: same class, same method name, DIFFERENT parameters — compile-time resolution (static polymorphism). Overriding: parent+child class, same name, SAME parameters — runtime resolution (dynamic polymorphism). Return type alone doesn't make overloading. Access modifier must be same or more permissive in override. @Override is mandatory annotation for overriding (catches typos). Static methods are hidden not overridden.
Q5. What is super() and what rule applies to it?
super() calls the parent class constructor from child constructor. Rule: must be the FIRST statement. If not explicitly written, Java implicitly inserts super() (no-arg parent constructor call). If parent has no no-arg constructor, you MUST explicitly call the correct super(args). super.method() calls parent's version of an overridden method. super.field accesses parent's field when child has same name.
Q6. What is the difference between protected and default access?
Default (no modifier): accessible within the same package only — subclasses in DIFFERENT packages cannot access it. Protected: accessible within same package AND in subclasses regardless of package. Example: if Vehicle.fuelType is protected, a Car class in a different package can access it. If it were default, it couldn't. In practice, use protected for "designed for inheritance" members.
Q7. Can you override a private method? Why or why not?
No. Private methods are not visible to child classes — they can't be inherited, so they can't be overridden. If a child class defines a method with the same name/signature as a parent's private method, it's a completely new method in the child class, not an override. @Override on it would cause a compile error. This is why you can't access private methods via polymorphism.
Q8. What does @Override annotation do?
@Override tells the compiler to verify that this method actually overrides a method from a superclass or interface. If the method doesn't match any parent method (wrong name spelling, wrong parameter type), the compiler throws an error. Without @Override, a typo like makeSound() vs makeSoud() would compile silently as a new method — the override never happens, causing subtle runtime bugs. Always use @Override when overriding.