Java Interview Questions
Java Inheritance - Theory Questions
1. What is inheritance in Java and what are its benefits?
Inheritance is an OOP concept where a class (subclass/child) acquires properties and behaviors of another class (superclass/parent). Benefits include:
- Code Reusability: Reuse existing code without rewriting
- Method Overriding: Runtime polymorphism
- Extensibility: Extend functionality of existing classes
- IS-A Relationship: Establishes natural hierarchy
- Maintainability: Changes in superclass automatically affect subclasses
2. What is the syntax for inheritance in Java?
Inheritance is implemented using the extends keyword:
class SuperClass {
// superclass members
}
class SubClass extends SuperClass {
// subclass members + inherited members
}
Java supports single inheritance for classes (one superclass per class).
3. What are the different types of inheritance in Java?
Single Inheritance: Class extends one superclass
Multilevel Inheritance: Class extends another subclass (A→B→C)
Hierarchical Inheritance: Multiple classes extend same superclass
Multiple Inheritance: NOT supported for classes (supported via interfaces)
Hybrid Inheritance: Combination of above (via interfaces)
4. Why doesn't Java support multiple inheritance for classes?
Java avoids multiple inheritance for classes due to:
- Diamond Problem: Ambiguity when same method exists in multiple parent classes
- Complexity: Makes language more complex and harder to maintain
- Simplified Design: Promotes cleaner, more maintainable code
Multiple inheritance is achieved through interfaces instead.
5. What is the diamond problem and how does Java handle it?
The diamond problem occurs when:
- Class D inherits from both B and C
- B and C both inherit from A
- If A has method m(), which m() should D inherit?
Java handles this by:
- Not allowing multiple inheritance for classes
- Allowing multiple inheritance through interfaces (Java 8+ with default methods)
- Providing conflict resolution rules for interface default methods
6. What is the super keyword and how is it used?
The super keyword refers to the immediate parent class. Uses:
- Access parent class members: super.variable or super.method()
- Call parent constructor: super() or super(parameters)
- Method overriding: Call overridden parent method
Example:
class Child extends Parent {
void display() {
super.display(); // call parent method
System.out.println("Child method");
}
}
7. What is constructor chaining in inheritance?
Constructor chaining is the process of calling parent constructors from child constructors:
- Child constructor automatically calls parent's no-arg constructor using super()
- If parent doesn't have no-arg constructor, must explicitly call super(parameters)
- super() must be first statement in child constructor
Example:
class Parent {
Parent(int x) { ... }
}
class Child extends Parent {
Child() {
super(10); // explicit call required
}
}
8. What members are inherited by subclasses?
Subclasses inherit:
- public and protected methods and variables
- default (package-private) members if in same package
Subclasses do NOT inherit:
- private members
- Constructors (but can call them)
- Initialization blocks (but they execute during object creation)
9. What is method overriding in inheritance?
Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. Rules:
- Same method signature (name and parameters)
- Return type should be same or covariant (subtype)
- Access modifier cannot be more restrictive
- Cannot override final, static, or private methods
- Use @Override annotation for clarity
10. What is the difference between method overloading and overriding?
Method Overloading:
- Same class, same method name, different parameters
- Compile-time polymorphism
- Return type can be different
- No inheritance required
Method Overriding:
- Different classes (inheritance), same method signature
- Runtime polymorphism
- Return type must be compatible
- Inheritance required
11. What is the Object class and its role in inheritance?
The Object class is the root of Java's class hierarchy. Every class implicitly extends Object if no other superclass is specified. Important methods:
- toString() - string representation
- equals() - object equality
- hashCode() - hash code value
- getClass() - runtime class
- clone() - object cloning
- finalize() - garbage collection (deprecated)
12. What is the final keyword in inheritance context?
The final keyword has different meanings:
- final class: Cannot be extended (inherited)
- final method: Cannot be overridden by subclasses
- final variable: Constant, cannot be reassigned
Examples: String, Integer, Math are final classes for security and immutability.
13. What is the instanceof operator and how is it used?
The instanceof operator checks if an object is an instance of a specific class or interface:
object instanceof ClassName
Returns true if:
- Object is instance of specified class
- Object is instance of subclass
- Object implements specified interface
Example:
if (animal instanceof Dog) {
Dog dog = (Dog) animal; // safe casting
}
14. What is dynamic method dispatch in Java?
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime rather than compile-time. This enables runtime polymorphism:
class Animal { void sound() { ... } }
class Dog extends Animal { void sound() { ... } }
class Cat extends Animal { void sound() { ... } }
Animal myAnimal = new Dog();
myAnimal.sound(); // Calls Dog's sound() at runtime
15. What is the difference between IS-A and HAS-A relationship?
IS-A Relationship (Inheritance):
- "Subclass IS-A Superclass"
- Achieved using extends keyword
- Represents inheritance hierarchy
- Example: Car IS-A Vehicle
HAS-A Relationship (Composition):
- "Class HAS-A Member"
- Achieved through instance variables
- Represents association/containment
- Example: Car HAS-A Engine
16. What is the difference between inheritance and composition?
Inheritance:
- "IS-A" relationship
- Code reuse through extending
- Tight coupling between classes
- Runtime polymorphism
Composition:
- "HAS-A" relationship
- Code reuse through object references
- Loose coupling between classes
- More flexible and testable
Prefer composition over inheritance for better design.
17. What are the access modifiers and their inheritance behavior?
public: Accessible everywhere, inherited by all subclasses
protected: Accessible within package and to subclasses (even in different packages)
default (package-private): Accessible only within same package, inherited by subclasses in same package
private: Not accessible outside class, not inherited by subclasses
18. What is covariant return type in method overriding?
Covariant return type allows overriding method to have return type that is subclass of the return type in parent method:
class Parent {
Parent getObject() { return new Parent(); }
}
class Child extends Parent {
@Override
Child getObject() { return new Child(); } // covariant return
}
Introduced in Java 5, provides more specific return types.
19. How does inheritance work with static members?
Static members (methods and variables) are inherited but:
- They belong to the class, not instances
- Cannot be overridden (but can be hidden)
- Accessed through class name (recommended) or instance
- Static method hiding occurs when subclass defines static method with same signature
Example:
class Parent { static void method() { ... } }
class Child extends Parent { static void method() { ... } } // method hiding
20. What are the best practices for using inheritance?
Design guidelines:
- Use inheritance only for true "IS-A" relationships
- Prefer composition over inheritance when possible
- Keep inheritance hierarchies shallow (2-3 levels)
- Use abstract classes for partial implementations
- Make classes final unless designed for extension
- Document methods designed for overriding
- Follow Liskov Substitution Principle
- Use @Override annotation consistently