HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
75 Topics
Chapter•390 Minutes

Java Progressive Master Question Bank

75 progressive Java interview questions complete with technical explanations, key takeaways, diagrams, code examples, and interactive knowledge check quizzes.

Q11: What are the Four Pillars of Object-Oriented Programming (OOP)?

  1. Encapsulation: Hiding state behind private fields, exposing access via controlled public methods.
  2. Abstraction: Hiding internal implementation complexity behind simple abstract interfaces or classes.
  3. Inheritance: Deriving new classes from parent classes to promote code reuse (extends).
  4. Polymorphism: Allowing objects to take multiple forms (Method Overloading and Method Overriding).
Key Takeaways
  • Encapsulation protects data integrity.
  • Abstraction simplifies user interaction contracts.
  • Inheritance models 'IS-A' hierarchies.
  • Polymorphism provides dynamic runtime dispatch.
Knowledge Check

Hiding implementation complexity and exposing only essential functional contracts is known as what?

Q12: How do Method Overloading and Method Overriding differ?

  • Overloading (Compile-Time / Static Polymorphism): Multiple methods in the same class share the same name but differ in parameter count or parameter types. Return type alone is insufficient.
  • Overriding (Runtime / Dynamic Polymorphism): Subclass provides a specific implementation of a method already declared in its superclass using the @Override annotation. Method signature and return type must match (or be covariant).
Key Takeaways
  • Overloading = Same method name, different parameter signature (Compile-Time).
  • Overriding = Same method signature in child class (Runtime Dispatch).
Knowledge Check

Can you overload a method by changing ONLY its return type in Java?

Q13: What are the differences between an Abstract Class and an Interface?

FeatureAbstract ClassInterface (Java 8+)
InheritanceSingle class inheritance (extends)Multiple interface implementation (implements)
StateCan hold state (private instance fields)Stateless (only public static final constants)
ConstructorsSupportedNot supported
MethodsAbstract, concrete, protected, private methodsabstract, default, static, and private (Java 9+) methods
Key Takeaways
  • Use Abstract Classes for sharing state and identity in class hierarchies.
  • Use Interfaces for defining peripheral behavioral capability contracts across unrelated hierarchies.
Knowledge Check

Can a class extend multiple abstract classes in Java?

Q14: What are Default, Static, and Private methods in Java Interfaces?

  • Default Methods (Java 8+): Methods declared with default keyword providing concrete method bodies. Enables API evolution without breaking existing interface implementations.
  • Static Methods (Java 8+): Utility methods attached directly to the interface scope.
  • Private Methods (Java 9+): Encapsulated helper methods used internally inside default or static interface methods.
Modern Interface Features
public interface Logger {
    void log(String message); // Abstract

    default void logWarning(String msg) { // Default
        formatAndPrint("WARN", msg);
    }

    private void formatAndPrint(String level, String msg) { // Private (Java 9+)
        System.out.println("[" + level + "]: " + msg);
    }
}
💡 Default methods provide implementation logic while private methods cleanly isolate helper logic.
Key Takeaways
  • default methods allow adding new interface methods safely.
  • private methods encapsulate shared internal logic inside interfaces.
Knowledge Check

Why were default methods introduced in Java 8?

Q15: How does Java resolve the Diamond Problem with Interface default methods?

If a class implements two interfaces that declare the exact same default method signature, the Java compiler raises a compile-time error due to ambiguity. The implementing class MUST resolve the conflict explicitly by overriding the method and selecting a specific interface using InterfaceName.super.methodName() or providing custom logic.

Resolving Default Method Diamond Ambiguity
interface A { default void show() { System.out.println("A"); } }
interface B { default void show() { System.out.println("B"); } }

class C implements A, B {
    @Override
    public void show() {
        A.super.show(); // Explicitly resolves ambiguity
    }
}
💡 C resolves the collision by explicitly invoking A's default implementation.
Key Takeaways
  • Ambiguous default methods force compile-time resolution.
  • Resolve using InterfaceName.super.methodName() inside the override.
Knowledge Check

What happens if class C implements Interface A and Interface B, which both contain an identical default method signature, without overriding it?

Q16: How does the static keyword operate on variables, methods, blocks, and nested classes?

  • Static Variables: Shared across all instances of a class; stored in heap class metadata area.
  • Static Methods: Bound to the class context. Cannot access instance fields (this / super).
  • Static Blocks: Execute once when the class is loaded into the JVM by the ClassLoader.
  • Static Nested Classes: Nested classes that do not hold an implicit reference to an outer class instance.
Key Takeaways
  • Static members belong to the class, not individual instance allocations.
  • Static blocks run during class loading before constructor invocations.
Knowledge Check

Can a static method access a non-static instance variable directly without instantiating an object?

Q17: What are the differences between final, finally, and finalize?

  • final: Keyword modifier.
    • final variable: Constant (value cannot be reassigned).
    • final method: Prevents method overriding.
    • final class: Prevents inheritance.
  • finally: Block paired with try-catch to execute cleanup code unconditionally.
  • finalize: Deprecated Object method invoked by Garbage Collector prior to object reclamation.
Key Takeaways
  • final restricts mutability/inheritance.
  • finally guarantees cleanup execution.
  • finalize() is deprecated—use Cleaner or AutoCloseable instead.
Knowledge Check

Will a finally block execute if a try block contains a return statement?

Q18: What is the visibility matrix for Java Access Modifiers?

ModifierSame ClassSame PackageSubclass (Diff Pkg)World (Diff Pkg)
privateYesNoNoNo
default (package-private)YesYesNoNo
protectedYesYesYesNo
publicYesYesYesYes
Key Takeaways
  • private: Lowest visibility (class scope). protected: Package scope + Subclasses across packages. default: Strictly package-wide scope.
Knowledge Check

Which access level allows access from a subclass located in a DIFFERENT package?

Q19: What is Composition versus Inheritance, and why favor Composition?

  • Inheritance (IS-A): Tightly couples subclass to superclass implementation details (class Dog extends Animal). Modifying parent behavior risks breaking derived subclasses.
  • Composition (HAS-A): Assembles complex objects by embedding component objects as private fields (class Car { private Engine engine; }).
  • Principle: Favor Composition over Inheritance to achieve loose coupling, runtime behavior swapping, and robust encapsulation.
Key Takeaways
  • Inheritance = IS-A relationship (tight coupling). Composition = HAS-A relationship (loose coupling, runtime flexibility).
Knowledge Check

Which design principle helps avoid brittle class hierarchies when extending framework features?

Q20: What are Covariant Return Types in method overriding?

Covariant Return Types allow an overriding method in a subclass to return a subtype of the return type declared in the superclass method. This removes the need for explicit client casting when calling derived methods.

Covariant Return Type Example
class SuperFactory {
    public Number produce() { return 100; }
}

class SubFactory extends SuperFactory {
    @Override
    public Integer produce() { // Covariant: Integer is a subtype of Number
        return 200;
    }
}
💡 SubFactory narrows the return type from Number to Integer legally.
Key Takeaways
  • Subclass overrides can narrow return types to more specific derived classes.
Knowledge Check

Can an overriding method return a supertype of the parent method's return type?