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)?
- Encapsulation: Hiding state behind
privatefields, exposing access via controlledpublicmethods. - Abstraction: Hiding internal implementation complexity behind simple abstract interfaces or classes.
- Inheritance: Deriving new classes from parent classes to promote code reuse (
extends). - Polymorphism: Allowing objects to take multiple forms (Method Overloading and Method Overriding).
- Encapsulation protects data integrity.
- Abstraction simplifies user interaction contracts.
- Inheritance models 'IS-A' hierarchies.
- Polymorphism provides dynamic runtime dispatch.
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
@Overrideannotation. Method signature and return type must match (or be covariant).
- Overloading = Same method name, different parameter signature (Compile-Time).
- Overriding = Same method signature in child class (Runtime Dispatch).
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?
| Feature | Abstract Class | Interface (Java 8+) |
|---|---|---|
| Inheritance | Single class inheritance (extends) | Multiple interface implementation (implements) |
| State | Can hold state (private instance fields) | Stateless (only public static final constants) |
| Constructors | Supported | Not supported |
| Methods | Abstract, concrete, protected, private methods | abstract, default, static, and private (Java 9+) methods |
- Use Abstract Classes for sharing state and identity in class hierarchies.
- Use Interfaces for defining peripheral behavioral capability contracts across unrelated hierarchies.
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
defaultkeyword 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.
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);
}
}defaultmethods allow adding new interface methods safely.privatemethods encapsulate shared internal logic inside interfaces.
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.
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
}
}- Ambiguous default methods force compile-time resolution.
- Resolve using
InterfaceName.super.methodName()inside the override.
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.
- Static members belong to the class, not individual instance allocations.
- Static blocks run during class loading before constructor invocations.
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 withtry-catchto execute cleanup code unconditionally.finalize: DeprecatedObjectmethod invoked by Garbage Collector prior to object reclamation.
finalrestricts mutability/inheritance.finallyguarantees cleanup execution.finalize()is deprecated—useCleanerorAutoCloseableinstead.
Will a finally block execute if a try block contains a return statement?
Q18: What is the visibility matrix for Java Access Modifiers?
| Modifier | Same Class | Same Package | Subclass (Diff Pkg) | World (Diff Pkg) |
|---|---|---|---|---|
private | Yes | No | No | No |
default (package-private) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
private: Lowest visibility (class scope).protected: Package scope + Subclasses across packages.default: Strictly package-wide scope.
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.
- Inheritance =
IS-Arelationship (tight coupling). Composition =HAS-Arelationship (loose coupling, runtime flexibility).
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.
class SuperFactory {
public Number produce() { return 100; }
}
class SubFactory extends SuperFactory {
@Override
public Integer produce() { // Covariant: Integer is a subtype of Number
return 200;
}
}- Subclass overrides can narrow return types to more specific derived classes.
Can an overriding method return a supertype of the parent method's return type?