Java OOP Interview Questions & Answers

Complete Interview Handbook • Classes • Objects • Encapsulation • Abstraction • Inheritance • Polymorphism • Constructors • Access Modifiers • OOP Design

560 questions with interview-ready explanations and practical Java guidance.

Interview approach: Start with the definition, explain the rule, give a small example, and then mention one real-world use case or pitfall.

Contents

1. OOP Fundamentals2. Class3. Object4. Object State, Behavior and Identity5. Encapsulation6. Abstraction7. Interfaces8. Inheritance9. Polymorphism10. Method Overloading11. Method Overriding12. Constructors13. this Keyword14. super Keyword15. Access Specifiers / Access Modifiers16. Non-Access Modifiers17. Static18. Final19. Abstract Modifier20. Inheritance + Constructor Tricky Questions21. Upcasting and Downcasting22. Object Class23. == vs equals()24. Composition / Aggregation / Association25. Immutable Objects26. OOP Tricky Output Questions27. OOP Coding Questions28. Scenario-Based OOP Questions29. Senior Java OOP Questions30. Most Frequently Asked Questions to Prioritize

1. OOP Fundamentals

Q1What is Object-Oriented Programming?
Answer: OOP is a programming paradigm that models software as objects containing state and behavior. Java is primarily object-oriented and uses classes, objects, inheritance, encapsulation, abstraction and polymorphism.
Q2What are the main principles of OOP?
Answer: The main principles are encapsulation, abstraction, inheritance and polymorphism.
Q3What are the four pillars of OOP?
Answer: Encapsulation hides and controls state; abstraction exposes essential behavior; inheritance enables reuse through an IS-A relationship; polymorphism allows one contract/reference to represent different implementations.
Q4What are the advantages of OOP?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q5What are the disadvantages of OOP?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q6How is OOP different from procedural programming?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q7What is an object?
Answer: An object is a runtime instance of a class. It has identity, state and behavior.
Q8What is a class?
Answer: A class is a blueprint that defines the state and behavior that objects created from it can have.
class Employee {
    private int id;
    private String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println(id + " - " + name);
    }
}

Employee e = new Employee(101, "John");
e.display();
Q9What is the difference between a class and an object?
Answer: A class is a definition/template; an object is a concrete runtime instance created from that class.
Q10What is an instance of a class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q11What is object state?
Answer: State is the current data held by an object, normally represented by its instance fields.
Q12What is object behavior?
Answer: Behavior is what an object can do, represented primarily by its methods.
Q13What is object identity?
Answer: Identity distinguishes one object instance from another, even when their state is identical.
Q14What are the properties/characteristics of an object?
Answer: The classic characteristics are identity, state and behavior.
Q15How is an object created in Java?
Answer: Commonly with `new`, for example `Employee e = new Employee();`. The constructor is then invoked to initialize the object.
Q16What happens internally when we use the new keyword?
Answer: Memory is allocated for the object, fields receive default values, the constructor runs, and a reference to the object is returned.
Q17Where are Java objects stored?
Answer: Objects are normally allocated in the heap. The exact implementation is JVM-dependent, but Java programmers generally reason about objects as heap allocations.
Q18What is a reference variable?
Answer: A reference variable stores a reference to an object rather than containing the object itself, for example `Employee e`.
Q19What is the difference between an object and an object reference?
Answer: The object is the runtime entity containing state; the reference is a variable that identifies/accesses that object.
Q20Can multiple references point to the same object?
Answer: Yes. Example: `Employee a = new Employee(); Employee b = a;` Both references point to the same object.
Q21Can an object exist without a reference?
Answer: Yes. An object can become unreachable when no live reference points to it; it can then become eligible for garbage collection.
Q22Can a reference exist without an object?
Answer: Yes. A reference can be `null`, meaning it currently refers to no object.
Q23What is an anonymous object?
Answer: An object created without storing its reference in a named variable, such as `new Employee().display();`.
Q24What is object initialization?
Answer: Initialization is the process of assigning initial state through field initializers, initialization blocks and constructors.
Q25What is object lifecycle?
Answer: It broadly covers creation, use, becoming unreachable, and eventual garbage collection. Java does not provide deterministic object destruction.

2. Class

Q26What is a class?
Answer: A class is a blueprint that defines the state and behavior that objects created from it can have.
class Employee {
    private int id;
    private String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void display() {
        System.out.println(id + " - " + name);
    }
}

Employee e = new Employee(101, "John");
e.display();
Q27How do you define a class in Java?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q28What can a Java class contain?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q29What are fields/variables in a class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q30What are methods in a class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q31What are constructors in a class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q32Can a class contain static members?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q33Can a class contain instance members?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q34Can a class contain both static and instance members?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q35Can a class be empty?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q36Can a class contain another class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q37What is a nested class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q38What is an inner class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q39What is a local class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q40What is an anonymous class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q41What is a top-level class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q42Can a Java class extend another class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q43Can a class implement multiple interfaces?
Answer: Yes.
Q44Can a class extend one class and implement multiple interfaces?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q45Can a class extend multiple classes?
Answer: No. A class can directly extend only one class.
Q46Can a class be both abstract and final?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q47Can a class be private?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q48Can a top-level class be protected?
Answer: No.
Q49Can a top-level class be public?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q50What is the difference between a concrete class and an abstract class?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.

3. Object

Q51What is an object?
Answer: An object is a runtime instance of a class. It has identity, state and behavior.
Q52How do you create an object?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q53What is the syntax for object creation?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q54What happens when an object is created?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q55What is the role of the new keyword?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q56Where is an object created?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q57Where is the reference stored?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q58Can one object have multiple references?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q59What happens when two references point to the same object?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q60What happens when an object loses all references?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q61What is garbage collection?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q62Can we explicitly destroy an object in Java?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q63What is an object's identity?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q64How can you compare two objects?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q65What is the difference between == and equals() for objects?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.

4. Object State, Behavior and Identity

Q66What is object state?
Answer: State is the current data held by an object, normally represented by its instance fields.
Q67What is object behavior?
Answer: Behavior is what an object can do, represented primarily by its methods.
Q68What is object identity?
Answer: Identity distinguishes one object instance from another, even when their state is identical.
Q69Give a real-world example of state, behavior and identity.
Answer: Use a familiar domain example. For instance, a bank account encapsulates balance; a payment interface abstracts payment processing; CreditCardPayment/UPIPayment provide polymorphic implementations; inheritance is used only where a true IS-A relationship exists.
Q70What is the difference between state and behavior?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q71How is state represented in Java?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q72How is behavior represented in Java?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q73How is object identity represented in Java?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q74Can two objects have the same state?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q75Can two objects have the same identity?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q76Can two different objects have the same values?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q77How do instance variables represent object state?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.

5. Encapsulation

Q78What is encapsulation?
Answer: Encapsulation bundles data and behavior and controls access to internal state. A common Java implementation uses private fields and controlled methods.
class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}
Q79Why is encapsulation important?
Answer: It protects invariants, reduces coupling, centralizes validation and allows implementation details to change without changing callers.
Q80How do you achieve encapsulation in Java?
Answer: Use appropriate access modifiers, usually private fields, and expose only required operations through methods.
Q81What is data hiding?
Answer: Data hiding means restricting direct access to implementation details, commonly using `private` members.
Q82What is the difference between encapsulation and data hiding?
Answer: Encapsulation is the broader bundling/control concept; data hiding specifically focuses on restricting direct visibility of internal details.
Q83Why are instance variables generally declared private?
Answer: To prevent uncontrolled external modification and force state changes through controlled APIs.
Q84What are getters and setters?
Answer: Getters read state and setters modify state. Setters can also validate or enforce business rules.
Q85Why do we use getters and setters?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q86Can we achieve encapsulation without getters and setters?
Answer: Yes. Encapsulation is about controlled access, not specifically getters/setters. Behavior-oriented methods can expose only valid operations.
Q87How can validation be implemented using encapsulation?
Answer: Keep the field private and validate input inside a constructor or controlled method before changing the state.
Q88How does encapsulation improve maintainability?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q89How does encapsulation improve security?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q90How does encapsulation reduce coupling?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q91What is a fully encapsulated class?
Answer: A class where instance fields are private and external access is provided through controlled methods or operations.
Q92How would you make a class properly encapsulated?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q93What problems can occur if fields are public?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q94Can a setter contain business logic?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q95Should every private field have a getter and setter?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q96What is immutable encapsulation?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q97How does encapsulation relate to Spring Boot applications?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q98How does encapsulation apply to DTOs and entities?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.

6. Abstraction

Q99What is abstraction?
Answer: Abstraction exposes what a component does while hiding unnecessary implementation details. Java commonly uses interfaces and abstract classes.
abstract class Vehicle {
    abstract void start();

    void stop() {
        System.out.println("Stopped");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car started");
    }
}
Q100Why do we need abstraction?
Answer: It reduces complexity, defines clear contracts, hides implementation details and makes systems easier to change and test.
Q101How can abstraction be achieved in Java?
Answer: Primarily through interfaces and abstract classes.
Q102What is an abstract class?
Answer: A class declared with `abstract`; it can contain abstract and concrete methods and cannot be instantiated directly.
Q103What is an abstract method?
Answer: A method declared without an implementation body. A concrete subclass normally must implement it.
Q104Can an abstract class contain concrete methods?
Answer: Yes.
Q105Can an abstract class contain variables?
Answer: Yes; it can contain instance, static, final and other fields subject to normal Java rules.
Q106Can an abstract class contain constructors?
Answer: Yes. Its constructor executes as part of constructing a concrete subclass object.
Q107Can an abstract class contain static methods?
Answer: Yes.
Q108Can an abstract class contain final methods?
Answer: Yes. A final method cannot be overridden.
Q109Can an abstract class contain private methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q110Can we create an object of an abstract class?
Answer: No, not directly. You create an object of a concrete subclass.
Q111Can an abstract class have zero abstract methods?
Answer: Yes. It can be abstract simply to prevent direct instantiation or to define a common base type.
Q112Can an abstract class extend another abstract class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q113Can an abstract class implement an interface?
Answer: Yes. It may implement some or all interface methods and leave the rest abstract.
Q114Can an abstract class extend a concrete class?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q115Can an abstract method be private?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q116Can an abstract method be static?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q117Can an abstract method be final?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q118What happens if a child class doesn't implement an abstract method?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q119What is the difference between abstraction and encapsulation?
Answer: Abstraction focuses on hiding unnecessary implementation complexity; encapsulation focuses on bundling state/behavior and controlling access to state.
Q120What is the difference between abstraction and data hiding?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q121Give a real-world example of abstraction.
Answer: Use a familiar domain example. For instance, a bank account encapsulates balance; a payment interface abstracts payment processing; CreditCardPayment/UPIPayment provide polymorphic implementations; inheritance is used only where a true IS-A relationship exists.

7. Interfaces

Q122What is an interface?
Answer: An interface is a reference type that defines a contract. Classes implement it and provide the required behavior.
Q123Why do we use interfaces?
Answer: Interfaces support abstraction, loose coupling, multiple inheritance of type and interchangeable implementations.
Q124How do you declare an interface?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q125Can an interface contain variables?
Answer: Yes. Interface fields are implicitly `public static final`.
Q126What is the default access level of interface variables?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q127Are interface variables final?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q128Are interface variables static?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q129Can an interface contain constructors?
Answer: No, because interfaces are not instantiated directly.
Q130Can an interface contain abstract methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q131Can an interface contain default methods?
Answer: Yes. Since Java 8, interfaces can provide `default` method implementations.
Q132Can an interface contain static methods?
Answer: Yes. Interface static methods belong to the interface and are called through the interface name.
Q133Can an interface contain private methods?
Answer: Yes, since Java 9. They can support code reuse among interface default/static methods.
Q134Can an interface contain protected methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q135Can an interface be instantiated?
Answer: No. An implementation object can be assigned to an interface reference.
Q136Can an interface extend another interface?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q137Can an interface extend multiple interfaces?
Answer: Yes.
Q138Can a class implement multiple interfaces?
Answer: Yes.
Q139Why does Java support multiple interfaces but not multiple class inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q140What is the diamond problem?
Answer: It is ambiguity caused when multiple inheritance provides conflicting implementations of the same method. Java avoids class multiple inheritance and defines rules for interface default-method conflicts.
Q141How does Java resolve conflicting default methods?
Answer: A class implementation wins over interface defaults; otherwise a more specific subinterface wins. If two unrelated interfaces conflict, the implementing class must resolve the conflict explicitly.
Q142Interface vs abstract class?
Answer: An interface defines a contract and supports multiple inheritance of type; an abstract class can share state, constructors and implemented behavior. Choice depends on whether you need a contract across unrelated classes or a shared base implementation.
Q143When would you choose an interface?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q144When would you choose an abstract class?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q145What is a functional interface?
Answer: An interface with exactly one abstract method, such as `Runnable`. It can be used as the target type of a lambda expression.
Q146What is a marker interface?
Answer: An interface with no methods used to mark a class for special handling, such as `Serializable`.
Q147What is a default method?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q148Why were default methods introduced?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

8. Inheritance

Q149What is inheritance?
Answer: Inheritance lets a subclass reuse and specialize accessible members of a superclass using `extends`.
Q150Why is inheritance used?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q151What is the extends keyword?
Answer: `extends` establishes class inheritance or interface inheritance.
Q152What are the advantages of inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q153What are the disadvantages of inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q154What is IS-A relationship?
Answer: IS-A represents inheritance/subtyping, such as `Dog IS-A Animal`.
Q155What is HAS-A relationship?
Answer: HAS-A represents composition or aggregation, such as `Car HAS-A Engine`.
Q156What are the types of inheritance?
Answer: Common structural forms are single, multilevel and hierarchical. Java does not support multiple inheritance between classes.
Q157What is single inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q158What is multilevel inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q159What is hierarchical inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q160Does Java support multiple inheritance?
Answer: Not for classes. A class can implement multiple interfaces.
Q161Why doesn't Java support multiple inheritance using classes?
Answer: It avoids ambiguity and complexity such as conflicting inherited implementations and the classic diamond problem.
Q162Can a class extend multiple classes?
Answer: No. A class can directly extend only one class.
Q163Can a class implement multiple interfaces?
Answer: Yes.
Q164Can an interface extend multiple interfaces?
Answer: Yes.
Q165Can a final class be inherited?
Answer: No.
Q166Can an abstract class be inherited?
Answer: Yes; inheritance is one of its primary uses.
Q167Can constructors be inherited?
Answer: No. Constructors belong to the class that declares them.
Q168Are private members inherited?
Answer: They are part of the superclass object state, but they are not directly accessible by the subclass and are not inherited as accessible members.
Q169Are static members inherited?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q170Are protected members inherited?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q171What happens when parent and child have variables with the same name?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q172What happens when parent and child have methods with the same signature?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q173What is method overriding in inheritance?
Answer: It occurs when a subclass provides a compatible implementation of an inherited instance method with the same signature.
Q174What is constructor chaining in inheritance?
Answer: It is the process by which subclass construction invokes a superclass constructor, directly or implicitly, before the subclass constructor body.
Q175What is the role of super in inheritance?
Answer: `super` accesses superclass members and invokes a superclass constructor with `super()`.

9. Polymorphism

Q176What is polymorphism?
Answer: Polymorphism allows one contract/reference to work with multiple concrete types. In Java, common forms are overloading and runtime overriding.
Q177What are the types of polymorphism?
Answer: Interview discussions commonly distinguish compile-time polymorphism (overloading) and runtime polymorphism (overriding/dynamic dispatch).
Q178What is compile-time polymorphism?
Answer: The compiler chooses among overloaded methods based on the compile-time types and arguments.
Q179What is runtime polymorphism?
Answer: For overridden instance methods, the JVM dispatches to the implementation belonging to the actual runtime object.
interface Payment {
    void pay(double amount);
}

class UPI implements Payment {
    public void pay(double amount) {
        System.out.println("UPI: " + amount);
    }
}

class Card implements Payment {
    public void pay(double amount) {
        System.out.println("Card: " + amount);
    }
}

Payment p = new UPI();
p.pay(1000);
Q180What is method overloading?
Answer: Defining multiple methods with the same name but different parameter lists.
class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}
Q181What is method overriding?
Answer: A subclass provides a compatible implementation for an inherited instance method.
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}
Q182What is the difference between overloading and overriding?
Answer: Overloading changes the parameter list and is resolved at compile time; overriding uses inheritance and compatible signatures and is dynamically dispatched for instance methods.
Q183Can methods be overloaded based only on return type?
Answer: No. Return type alone does not distinguish a method signature.
Q184Can constructors be overloaded?
Answer: Yes, by providing different parameter lists.
Q185Can static methods be overloaded?
Answer: Yes.
Q186Can private methods be overloaded?
Answer: Yes, within the declaring class.
Q187Can final methods be overloaded?
Answer: Yes. `final` prevents overriding, not overloading.
Q188Can static methods be overridden?
Answer: No. Static methods are hidden rather than overridden.
Q189Can private methods be overridden?
Answer: No. They are not inherited/visible to the subclass for overriding.
Q190Can final methods be overridden?
Answer: No.
Q191Can constructors be overridden?
Answer: No. Constructors are not inherited.
Q192What is dynamic method dispatch?
Answer: It is runtime selection of an overridden instance method based on the actual object.
Q193What is late binding?
Answer: Binding of an overridden instance method to its implementation at runtime.
Q194What is early binding?
Answer: Binding resolved at compile time, commonly associated with overloaded, static, private and final methods.
Q195What is static binding?
Answer: Compile-time method resolution used for methods that do not participate in runtime overriding, such as static and private methods.
Q196What is dynamic binding?
Answer: Runtime resolution of an overridden instance method.
Q197What is runtime method resolution?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q198What is compile-time method resolution?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q199What is covariant return type?
Answer: When overriding, the child method may return a subtype of the parent's return type.
Q200Can an overridden method have a different return type?
Answer: Only if the return type is the same or covariant for reference types; primitives cannot use covariant returns.
Q201Can an overridden method reduce access visibility?
Answer: No. It cannot be more restrictive than the overridden method.
Q202Can an overridden method increase access visibility?
Answer: Yes.
Q203Can an overriding method throw broader checked exceptions?
Answer: No. It cannot introduce broader checked exceptions than the parent method.
Q204Can an overriding method throw narrower checked exceptions?
Answer: Yes.
Q205Can an overriding method throw unchecked exceptions?
Answer: Yes. Unchecked exceptions are not restricted by the checked-exception overriding rule.
Q206What is method hiding?
Answer: When a subclass declares a static method with the same signature as a superclass static method, the child method hides rather than overrides it.
Q207Method overriding vs method hiding?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.

10. Method Overloading

Q208What is method overloading?
Answer: Defining multiple methods with the same name but different parameter lists.
class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}
Q209Why is method overloading called compile-time polymorphism?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q210What are the rules for method overloading?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q211Can overloaded methods have different return types?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q212Can overloaded methods have different access modifiers?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q213Can overloaded methods be static?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q214Can overloaded methods be final?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q215Can overloaded methods be private?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q216Can methods be overloaded by changing only parameter names?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q217Can methods be overloaded by changing only return type?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q218Can methods be overloaded with different exceptions?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q219Can constructors be overloaded?
Answer: Yes, by providing different parameter lists.
Q220Can main() be overloaded?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q221How does Java select an overloaded method?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q222What is method resolution during overloading?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q223How does method overloading work with primitive types?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q224How does method overloading work with autoboxing?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q225How does method overloading work with varargs?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.

11. Method Overriding

Q226What is method overriding?
Answer: A subclass provides a compatible implementation for an inherited instance method.
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}
Q227What are the rules for method overriding?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q228Can a static method be overridden?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q229Can a private method be overridden?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q230Can a final method be overridden?
Answer: No.
Q231Can an abstract method be overridden?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q232Can an overloaded method also be overridden?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q233Can the return type be changed while overriding?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q234What is a covariant return type?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q235Can access visibility be reduced?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q236Can access visibility be increased?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q237Can an overriding method throw checked exceptions?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q238What happens if the child method throws a broader checked exception?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q239Can an overriding method throw unchecked exceptions?
Answer: Yes. Unchecked exceptions are not restricted by the checked-exception overriding rule.
Q240How does @Override help?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q241Is @Override mandatory?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q242What happens if the method signature is slightly different?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q243What is accidental method overloading instead of overriding?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

12. Constructors

Q244What is a constructor?
Answer: A constructor is a special class member used to initialize a new object. It has the class name and no return type.
class Employee {
    private final int id;
    private final String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
Q245Why do we need constructors?
Answer: They establish a valid initial state when an object is created.
Q246What are the characteristics of constructors?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q247What is a default constructor?
Answer: The compiler supplies a no-argument constructor only when the class declares no constructor.
Q248What is a no-argument constructor?
Answer: Any constructor that accepts zero arguments, whether written by the programmer or generated by the compiler.
Q249What is a parameterized constructor?
Answer: A constructor that accepts one or more parameters to initialize object state.
Q250What is constructor overloading?
Answer: Defining multiple constructors with different parameter lists.
Q251Can constructors be overloaded?
Answer: Yes, by providing different parameter lists.
Q252Can constructors be overridden?
Answer: No. Constructors are not inherited.
Q253Can constructors be inherited?
Answer: No. Constructors belong to the class that declares them.
Q254Can constructors be private?
Answer: Yes. Private constructors restrict direct construction and are used in patterns such as utility/singleton-style designs.
Q255Can constructors be protected?
Answer: Yes.
Q256Can constructors be public?
Answer: Yes.
Q257Can constructors be static?
Answer: No.
Q258Can constructors be final?
Answer: No.
Q259Can constructors be abstract?
Answer: No.
Q260Can a constructor return a value?
Answer: No. Constructors have no return type.
Q261Can a constructor call another constructor?
Answer: Yes, using `this(...)` for another constructor in the same class or `super(...)` for a superclass constructor.
Q262What is constructor chaining?
Answer: Calling one constructor from another to centralize initialization.
Q263What is this()?
Answer: `this(...)` invokes another constructor in the same class and must be the first statement.
class Employee {
    Employee() {
        this(101);
    }

    Employee(int id) {
        System.out.println(id);
    }
}
Q264What is super()?
Answer: `super(...)` invokes a superclass constructor and must be the first statement of the constructor.
class Parent {
    Parent(int x) {
        System.out.println("Parent: " + x);
    }
}

class Child extends Parent {
    Child() {
        super(10);
    }
}
Q265What is the difference between this() and super()?
Answer: `this()` calls another constructor in the same class; `super()` calls a superclass constructor.
Q266Where must this() appear?
Answer: As the first statement of a constructor.
Q267Where must super() appear?
Answer: As the first statement of a constructor.
Q268Can this() and super() be used together in the same constructor?
Answer: No, because both must be the first statement.
Q269What happens if we don't explicitly call super()?
Answer: The compiler inserts an implicit no-argument `super()` if an accessible no-argument superclass constructor exists.
Q270What happens if the parent class doesn't have a no-argument constructor?
Answer: The child constructor must explicitly invoke an accessible parent constructor with matching arguments.
Q271Can a constructor call a method?
Answer: Yes, although calling overridable methods from constructors is generally discouraged because subclass state may not yet be initialized.
Q272Can a constructor be synchronized?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q273What happens during object creation and constructor execution?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

13. this Keyword

Q274What is the this keyword?
Answer: `this` refers to the current object instance.
Q275Why do we use this?
Answer: It disambiguates instance fields from parameters, invokes same-class methods/constructors, passes the current object, and can return the current object.
Q276What does this refer to?
Answer: The current object whose instance method or constructor is executing.
Q277How can this differentiate instance variables from parameters?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q278Can this be used to call another method?
Answer: Yes, explicitly with `this.method()` or implicitly by calling the method directly.
Q279Can this be passed as an argument?
Answer: Yes.
Q280Can this be returned from a method?
Answer: Yes, commonly for fluent APIs.
Q281What is this()?
Answer: `this(...)` invokes another constructor in the same class and must be the first statement.
class Employee {
    Employee() {
        this(101);
    }

    Employee(int id) {
        System.out.println(id);
    }
}
Q282What is constructor chaining using this()?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q283Can this() and super() be used together?
Answer: Not in the same constructor because both must be first.
Q284Can this be used inside a static method?
Answer: No. Static methods have no current object instance.
Q285What happens if we try to use this inside a static method?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

14. super Keyword

Q286What is the super keyword?
Answer: `super` refers to the immediate superclass portion of the current object.
Q287Why do we use super?
Answer: To access superclass fields/methods and invoke superclass constructors.
Q288How do you access a parent variable using super?
Answer: Use `super.fieldName`.
Q289How do you call a parent method using super?
Answer: Use `super.methodName()`.
Q290How do you call a parent constructor using super()?
Answer: Use `super(arguments)` as the first statement of the child constructor.
Q291What is the difference between this and super?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q292Can super be used inside a static method?
Answer: No, because `super` requires a current object instance.
Q293Can super() be used anywhere inside a constructor?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q294What happens if the parent has no default constructor?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q295Can we use both this() and super() in one constructor?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

15. Access Specifiers / Access Modifiers

Q296What are access modifiers in Java?
Answer: They control visibility: `private`, package-private/default, `protected`, and `public`.
Q297What are the four access levels in Java?
Answer: Private, package-private/default, protected and public.
Q298What is private?
Answer: Accessible only within the declaring top-level class.
Q299What is default/package-private access?
Answer: When no access modifier is specified, the member is accessible within the same package.
Q300What is protected?
Answer: Accessible in the same package and in subclasses in other packages, subject to Java's protected access rules.
Q301What is public?
Answer: Accessible wherever the containing type/member is accessible.
Q302Which access modifier is most restrictive?
Answer: `private`.
Q303Which access modifier provides the widest access?
Answer: `public`.
Q304What is the difference between default and protected?
Answer: Both allow same-package access, but protected also provides inherited access to subclasses in other packages.
Q305What is the difference between protected and public?
Answer: Protected has package/subclass restrictions; public has no access restriction at the member level.
Q306Can a top-level class be private?
Answer: No. Top-level classes can be public or package-private; nested classes can be private.
Q307Can a top-level class be protected?
Answer: No.
Q308Can a nested class be private?
Answer: Yes.
Q309Can a constructor be private?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q310Can a constructor be protected?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q311Can an interface method be private?
Answer: Yes, since Java 9, for helper methods inside the interface.
Q312Can an interface method be protected?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q313Can interface fields have private access?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q314How does protected access work across packages?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q315Can a subclass access a protected member through a parent reference?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q316What happens when a method's visibility is reduced during overriding?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.

16. Non-Access Modifiers

Q317What are non-access modifiers?
Answer: Modifiers that change behavior rather than basic visibility, such as `static`, `final`, `abstract`, `synchronized`, `volatile`, `transient` and `native`.
Q318What is static?
Answer: `static` makes a member belong to the class rather than each object.
Q319What is final?
Answer: `final` prevents reassignment of a variable, overriding of a method, or inheritance of a class.
Q320What is abstract?
Answer: `abstract` declares a class/method as incomplete and intended for subclass implementation or specialization.
Q321What is synchronized?
Answer: `synchronized` provides monitor-based mutual exclusion and establishes memory-visibility guarantees around synchronized execution.
Q322What is volatile?
Answer: `volatile` gives visibility/order guarantees for a variable across threads but does not make compound operations atomic.
Q323What is transient?
Answer: `transient` marks an instance field to be skipped by Java's default serialization mechanism.
Q324What is native?
Answer: `native` declares a method implemented outside Java, typically through JNI.
Q325What is strictfp?
Answer: Historically, `strictfp` requested strict floating-point semantics. Since modern Java versions floating-point evaluation is consistently strict, so its practical importance is reduced.
Q326What is the difference between access and non-access modifiers?
Answer: Access modifiers control visibility; non-access modifiers change behavior, lifecycle, inheritance, synchronization or other semantics.
Q327Which modifiers can be applied to classes?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q328Which modifiers can be applied to methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q329Which modifiers can be applied to variables?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q330Which combinations of modifiers are invalid?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.

17. Static

Q331What is static?
Answer: `static` makes a member belong to the class rather than each object.
Q332What is a static variable?
Answer: A class variable shared by all instances of that class.
Q333What is a static method?
Answer: A method associated with the class rather than a particular object.
Q334What is a static block?
Answer: A class initialization block executed when the class is initialized.
Q335Why do we use static members?
Answer: For state or behavior that logically belongs to the class as a whole, such as constants or utility operations.
Q336What is the difference between static and instance variables?
Answer: A static variable has one class-level storage concept shared by instances; each object has its own instance fields.
Q337What is the difference between static and instance methods?
Answer: Static methods have no implicit `this` and are class-level; instance methods execute against an object.
Q338Can a static method access instance variables directly?
Answer: No. It needs an object/reference because there is no implicit current instance.
Q339Can an instance method access static variables?
Answer: Yes.
Q340Can a static method access static variables?
Answer: Yes.
Q341Can a static block access instance variables?
Answer: Not directly, because a static block has no implicit object.
Q342Can we override static methods?
Answer: No. They are hidden.
Q343Can we overload static methods?
Answer: Yes.
Q344Can static methods be inherited?
Answer: They can be inherited as class members subject to normal access, but calls are statically resolved.
Q345When is a static block executed?
Answer: During class initialization, before the class is actively used; exact initialization timing follows JVM class-initialization rules.
Q346How many times is a static block executed?
Answer: Normally once per class initialization by a given class loader.
Q347Can we create an object without calling a static block?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q348What is static initialization?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q349What is a static nested class?
Answer: A nested class declared `static`; unlike a non-static inner class, it does not require an enclosing instance.
Q350Can a static class exist at the top level?
Answer: No. The `static` modifier applies to nested classes, not top-level classes.

18. Final

Q351What is the final keyword?
Answer: `final` restricts reassignment, overriding or inheritance depending on where it is applied.
Q352What is a final variable?
Answer: A variable that can be assigned only once after definite initialization.
Q353What is a final method?
Answer: A method that subclasses cannot override.
Q354What is a final class?
Answer: A class that cannot be extended.
Q355Can a final variable be initialized in a constructor?
Answer: Yes, an instance final field can be initialized in a constructor, provided it is definitely assigned exactly once.
Q356What is a blank final variable?
Answer: A final field declared without an initializer and assigned later exactly once, commonly in a constructor.
Q357Can a final reference point to a mutable object?
Answer: Yes. `final` prevents changing the reference, not mutating the referenced object's internal state.
Q358Can a final object be modified?
Answer: There is no 'final object' keyword; a final reference can still refer to a mutable object whose state may change.
Q359Can a final method be overloaded?
Answer: Yes.
Q360Can a final method be overridden?
Answer: No.
Q361Can a final class be inherited?
Answer: No.
Q362Why is String final?
Answer: String's immutability and finality support security, caching, string-pool behavior and reliable use as a value/key type.
Q363Can an abstract class be final?
Answer: No. An abstract class requires subclassing for instantiation, while final prevents subclassing.
Q364What is the difference between final, finally and finalize?
Answer: `final` is a modifier; `finally` is an exception-handling block; `finalize()` was a legacy GC-related method and is deprecated for removal and should not be used for resource management.

19. Abstract Modifier

Q365What is the abstract keyword?
Answer: It marks a class as non-instantiable or a method as requiring implementation by a suitable subclass.
Q366What is an abstract class?
Answer: A class declared with `abstract`; it can contain abstract and concrete methods and cannot be instantiated directly.
Q367What is an abstract method?
Answer: A method declared without an implementation body. A concrete subclass normally must implement it.
Q368Can an abstract class be instantiated?
Answer: No, not directly.
Q369Can an abstract class have constructors?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q370Can an abstract class have static methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q371Can an abstract class have final methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q372Can an abstract class have private methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q373Can a class contain both abstract and concrete methods?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q374Can an abstract method be static?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q375Can an abstract method be final?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q376Can an abstract method be private?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q377Can an abstract class implement an interface?
Answer: Yes. It may implement some or all interface methods and leave the rest abstract.

20. Inheritance + Constructor Tricky Questions

Q378What is constructor execution order in inheritance?
Answer: Superclass construction occurs before subclass construction. Static initialization occurs before instance construction, according to class initialization rules.
Q379Which constructor executes first: parent or child?
Answer: The parent constructor executes before the child constructor body.
Q380Why is the parent constructor executed first?
Answer: The superclass portion of the object must be initialized before the subclass-specific initialization.
Q381What happens when super() is not written?
Answer: An implicit no-argument superclass constructor invocation is inserted if possible.
Q382What happens if the parent has only a parameterized constructor?
Answer: The child must explicitly call one of those accessible parameterized constructors.
Q383Can a child constructor call a parameterized parent constructor?
Answer: Yes, using `super(arguments)`.
Q384What is constructor chaining?
Answer: Calling one constructor from another to centralize initialization.
Q385What is the difference between constructor chaining and method overriding?
Answer: Constructor chaining controls initialization across constructors; overriding replaces inherited instance-method behavior and is dynamically dispatched.
Q386Can constructors be inherited?
Answer: No. Constructors belong to the class that declares them.
Q387Can constructors be overridden?
Answer: No. Constructors are not inherited.
Q388What happens when a child object is created?
Answer: Class initialization occurs as required; memory is allocated, fields receive defaults, superclass constructors execute, then subclass initialization and constructor code execute.
Q389In what order are static blocks, instance blocks and constructors executed?
Answer: Static initialization happens once when classes initialize. For an object, superclass instance initialization/constructor runs before subclass instance initialization/constructor. Within a class, field initializers and instance initializer blocks follow source order before the constructor body.

21. Upcasting and Downcasting

Q390What is upcasting?
Answer: Treating a subclass object through a superclass or interface reference, e.g. `Animal a = new Dog();`. It is implicit.
Q391What is downcasting?
Answer: Explicitly casting a superclass/interface reference back to a more specific subtype, e.g. `Dog d = (Dog) a;`.
Q392Why is upcasting automatic?
Answer: Every valid subtype object is also an instance of its supertype, so the conversion is type-safe.
Q393Why is downcasting explicit?
Answer: Not every superclass reference refers to the requested subtype, so Java requires an explicit cast and runtime check.
Q394What is the risk of downcasting?
Answer: If the runtime object is not compatible with the target type, a `ClassCastException` occurs.
Q395What is ClassCastException?
Answer: A runtime exception thrown when an object reference is cast to an incompatible type.
Q396What is instanceof?
Answer: An operator that tests whether an object is compatible with a given reference type at runtime.
Q397How can instanceof prevent ClassCastException?
Answer: Check the runtime type before casting, for example `if (a instanceof Dog d) { ... }`.
Q398Can an interface reference point to an implementation object?
Answer: Yes. This is a common way to achieve loose coupling and runtime polymorphism.
Q399What methods can be accessed through a parent reference?
Answer: Methods available in the reference type. Overridden instance methods dispatch to the actual object; child-only methods require a valid downcast.
Q400What happens when an overridden method is called through a parent reference?
Answer: The child's overridden implementation executes at runtime.

22. Object Class

Q401What is java.lang.Object?
Answer: The root superclass of all Java classes. It defines common methods such as `equals`, `hashCode` and `toString`.
Q402Why is Object considered the root class?
Answer: Every class ultimately extends Object directly or indirectly unless it is an interface.
Q403What are the important methods of Object?
Answer: Common interview methods include `equals`, `hashCode`, `toString`, `getClass`, `clone`, `wait`, `notify` and `notifyAll`.
Q404What is toString()?
Answer: A method that returns a string representation of an object. It is commonly overridden for useful logging/debugging output.
Q405What is equals()?
Answer: A method used for logical equality. Object's default implementation is identity-based; value classes such as String override it.
Q406What is hashCode()?
Answer: It returns an integer hash used by hash-based collections. Equal objects must have equal hash codes.
Q407What is getClass()?
Answer: It returns the runtime `Class` object representing the object's actual class.
Q408What is clone()?
Answer: A legacy mechanism for object copying through `Cloneable` and `Object.clone()`. It has pitfalls, so copy constructors/factories are often clearer.
Q409What are wait(), notify() and notifyAll()?
Answer: Object monitor methods used for thread coordination. They must be used with the object's monitor, typically inside synchronized code.
Q410What is the contract between equals() and hashCode()?
Answer: If `a.equals(b)` is true, `a.hashCode()` must equal `b.hashCode()`. Unequal objects may still share a hash code.
Q411Why should hashCode() be overridden when equals() is overridden?
Answer: Hash-based collections rely on both methods. Equal objects must land in the same hash bucket semantics.
Q412What happens if equals() is overridden but hashCode() isn't?
Answer: Equal objects may have different hash codes and behave incorrectly in `HashMap`, `HashSet` and related collections.

23. == vs equals()

Q413What is the difference between == and equals()?
Answer: For primitives, `==` compares values. For references, `==` compares whether references point to the same object. `equals()` can implement logical equality.
Q414How does == work with primitives?
Answer: It compares primitive values after applicable numeric conversions.
Q415How does == work with objects?
Answer: It compares reference identity, not object content.
Q416How does equals() work?
Answer: Its behavior depends on the class's implementation. Object uses identity semantics; many value classes override it for content equality.
Q417Why does String override equals()?
Answer: So two String objects containing the same character sequence are logically equal.
Q418What happens when two different objects have the same data?
Answer: They can be logically equal if their class defines `equals()` that way, while `==` remains false because their identities differ.
Q419How do you compare objects correctly?
Answer: Use `==` for identity/reference comparison and a properly implemented `equals()` for logical equality.
Q420Why is overriding equals() important in collections?
Answer: Collections such as `HashSet` and `HashMap` use equality/hash codes to determine logical uniqueness and key lookup.

24. Composition / Aggregation / Association

Q421What is association?
Answer: A general relationship between objects, such as a Teacher associated with Students.
Q422What is aggregation?
Answer: A weaker whole-part relationship where the contained object can exist independently of the container.
Q423What is composition?
Answer: A stronger whole-part relationship where the containing object owns the lifecycle/meaning of the contained part.
Q424What is the difference between association and aggregation?
Answer: Association is a general relationship; aggregation specifically models a weak whole-part relationship.
Q425What is the difference between aggregation and composition?
Answer: Aggregation allows independent lifetime; composition implies stronger ownership and lifecycle dependency.
Q426What is HAS-A relationship?
Answer: HAS-A represents composition or aggregation, such as `Car HAS-A Engine`.
Q427What is IS-A relationship?
Answer: IS-A represents inheritance/subtyping, such as `Dog IS-A Animal`.
Q428Inheritance vs composition?
Answer: Inheritance models IS-A and shares a subtype contract; composition models HAS-A and assembles behavior. Composition often reduces coupling and is easier to vary.
Q429Why is composition often preferred over inheritance?
Answer: It avoids deep hierarchies, reduces coupling to superclass implementation and makes behavior easier to replace or combine.
Q430Give a real-world example of composition.
Answer: Use a familiar domain example. For instance, a bank account encapsulates balance; a payment interface abstracts payment processing; CreditCardPayment/UPIPayment provide polymorphic implementations; inheritance is used only where a true IS-A relationship exists.
Q431Give a real-world example of aggregation.
Answer: Use a familiar domain example. For instance, a bank account encapsulates balance; a payment interface abstracts payment processing; CreditCardPayment/UPIPayment provide polymorphic implementations; inheritance is used only where a true IS-A relationship exists.
Q432Give a real-world example of association.
Answer: Use a familiar domain example. For instance, a bank account encapsulates balance; a payment interface abstracts payment processing; CreditCardPayment/UPIPayment provide polymorphic implementations; inheritance is used only where a true IS-A relationship exists.

25. Immutable Objects

Q433What is an immutable object?
Answer: An object whose observable state cannot change after construction.
final class Employee {
    private final int id;
    private final String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() { return id; }
    public String getName() { return name; }
}
Q434What is an immutable class?
Answer: A class designed so instances cannot change their externally observable state after construction.
Q435How do you create an immutable class?
Answer: Use private final state, initialize it completely in constructors, provide no mutators, prevent subclass state changes where appropriate, and defensively copy mutable inputs/outputs.
Q436Why is String immutable?
Answer: Immutability supports safe sharing, string pooling, stable hashing, security and predictable behavior.
Q437What are the advantages of immutability?
Answer: Thread-safety benefits, simpler reasoning, safe sharing, stable hash codes and fewer accidental state changes.
Q438What are the disadvantages of immutability?
Answer: Repeated modifications may create new objects and can increase allocation/copying costs for some workloads.
Q439What is defensive copying?
Answer: Creating a copy of mutable input/output data so callers cannot change an object's internal state through an external reference.
Q440Why should mutable fields be handled carefully in immutable classes?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q441Can an immutable class contain mutable objects?
Answer: Yes, but it must protect them with defensive copies and must not expose mutable internal references.
Q442What is the difference between immutable and final?
Answer: `final` is a language modifier; immutability is an object-design property. A final reference can point to a mutable object.
Q443Is a final reference immutable?
Answer: No. The reference cannot be reassigned, but the referenced object may be mutable.
Q444How would you design an immutable Employee class?
Answer: Make the class final where appropriate, fields private final, initialize them in the constructor, provide getters without setters, and defensively copy mutable fields.

26. OOP Tricky Output Questions

Q445What happens when a parent reference points to a child object?
Answer: The reference can access members visible in the parent type, while overridden instance methods dispatch to the child implementation.
Q446Which method executes in runtime polymorphism?
Answer: The most specific overridden implementation corresponding to the actual runtime object.
Q447Which variable is accessed when parent and child have the same field name?
Answer: Fields are hidden rather than dynamically overridden; access is determined by the reference's compile-time type.
Q448What happens when static methods have the same signature in parent and child?
Answer: The child static method hides the parent method; the selected method is based on the reference/class used for the call.
Q449What happens when private methods have the same signature?
Answer: They are separate methods because private methods are not inherited for overriding.
Q450What happens when final methods are redeclared in child classes?
Answer: A same-signature declaration intended as an override causes a compile-time error. A method with a different signature is simply an overload.
Q451What happens when constructors are overloaded?
Answer: Different constructors are selected based on the argument list at compile time.
Q452What happens when this() is used?
Answer: Another constructor in the same class executes first, then control returns to the caller constructor.
Q453What happens when super() is used?
Answer: The selected superclass constructor executes before the current constructor body.
Q454What happens when a child object is assigned to a parent reference?
Answer: That is upcasting and is valid because the child is-a parent.
Q455What happens when a parent object is cast to a child?
Answer: The cast compiles if the types are related, but at runtime it succeeds only when the actual object is compatible with the child type.
Q456When does ClassCastException occur?
Answer: When a reference is explicitly cast to an incompatible runtime type.
Q457What happens when a final reference points to a mutable object?
Answer: The reference cannot point elsewhere, but the object's mutable state can still change.
Q458What happens when equals() is overridden without hashCode()?
Answer: Hash-based collections can violate expected lookup/uniqueness behavior because equal objects may have different hashes.
Q459What happens when overloaded methods accept int, long, Integer, and Object?
Answer: Java chooses the most specific applicable overload using its compile-time overload-resolution rules, considering widening, boxing/unboxing and reference conversion.
Q460How does Java select the correct overloaded method?
Answer: The compiler uses the argument expressions' compile-time types and the overload-resolution rules; runtime object type is not used to select overloaded methods.

27. OOP Coding Questions

Q461Write a program demonstrating class and object.
Answer: Implement a small Java example focused on class and object. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q462Write a program demonstrating encapsulation.
Answer: Implement a small Java example focused on encapsulation. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q463Write a program demonstrating abstraction.
Answer: Implement a small Java example focused on abstraction. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q464Write a program demonstrating inheritance.
Answer: Implement a small Java example focused on inheritance. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q465Write a program demonstrating polymorphism.
Answer: Implement a small Java example focused on polymorphism. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q466Write a program demonstrating method overloading.
Answer: Implement a small Java example focused on method overloading. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q467Write a program demonstrating method overriding.
Answer: Implement a small Java example focused on method overriding. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q468Write a program demonstrating constructor overloading.
Answer: Implement a small Java example focused on constructor overloading. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q469Write a program demonstrating constructor chaining.
Answer: Implement a small Java example focused on constructor chaining. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q470Write a program demonstrating this().
Answer: Implement a small Java example focused on this(). For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q471Write a program demonstrating super().
Answer: Implement a small Java example focused on super(). For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q472Write a program demonstrating upcasting.
Answer: Implement a small Java example focused on upcasting. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q473Write a program demonstrating downcasting.
Answer: Implement a small Java example focused on downcasting. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q474Write a program demonstrating instanceof.
Answer: Implement a small Java example focused on instanceof. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q475Write a program demonstrating composition.
Answer: Implement a small Java example focused on composition. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q476Write a program demonstrating aggregation.
Answer: Implement a small Java example focused on aggregation. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q477Write a program demonstrating interface-based programming.
Answer: Implement a small Java example focused on interface-based programming. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q478Write a program demonstrating multiple interfaces.
Answer: Implement a small Java example focused on multiple interfaces. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q479Write a program demonstrating runtime polymorphism.
Answer: Implement a small Java example focused on runtime polymorphism. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q480Write a program demonstrating immutable class design.
Answer: Implement a small Java example focused on immutable class design. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q481Write a program overriding equals() and hashCode().
Answer: Implement a small Java example focused on als() and hashCode(). For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q482Write a program overriding toString().
Answer: Implement a small Java example focused on tring(). For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q483Write a program demonstrating static members.
Answer: Implement a small Java example focused on static members. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q484Write a program demonstrating final variables.
Answer: Implement a small Java example focused on final variables. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q485Write a program demonstrating final methods.
Answer: Implement a small Java example focused on final methods. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.
Q486Write a program demonstrating final classes.
Answer: Implement a small Java example focused on final classes. For interview practice, explain the class/interface design, the key keyword involved, and the expected output. A representative pattern is shown below.

28. Scenario-Based OOP Questions

Q487Design a payment system using OOP.
Answer: Model the main domain concepts as classes/interfaces for a payment system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q488Design a notification system using OOP.
Answer: Model the main domain concepts as classes/interfaces for a notification system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q489Design an employee management system using OOP.
Answer: Model the main domain concepts as classes/interfaces for an employee management system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q490Design a banking system using OOP.
Answer: Model the main domain concepts as classes/interfaces for a banking system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q491Design an order management system using OOP.
Answer: Model the main domain concepts as classes/interfaces for an order management system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q492Design a vehicle management system using OOP.
Answer: Model the main domain concepts as classes/interfaces for a vehicle management system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q493Design a library management system using OOP.
Answer: Model the main domain concepts as classes/interfaces for a library management system using OOP. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q494Design an e-commerce product hierarchy.
Answer: Model the main domain concepts as classes/interfaces for an e-commerce product hierarchy. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q495Design a customer and account relationship.
Answer: Model the main domain concepts as classes/interfaces for a customer and account relationship. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q496Design an application supporting multiple payment methods.
Answer: Model the main domain concepts as classes/interfaces for an application supporting multiple payment methods. Keep state private, expose behavior through methods, use interfaces for replaceable behavior, and prefer composition where inheritance is not an IS-A relationship.
Q497How would you use abstraction in a payment system?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q498How would you use polymorphism in a notification system?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q499How would you use encapsulation in a bank account?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q500How would you use composition in an order management system?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.

29. Senior Java OOP Questions

Q501Why should we favor composition over inheritance?
Answer: Yes/No or definition depends on the exact Java rule involved. State the rule first, then give a small example. For this topic, focus on Java's type system, inheritance rules, access control, object lifecycle and compile-time versus runtime method resolution.
Q502What is tight coupling?
Answer: Tight coupling means a component depends strongly on concrete implementation details, making change and testing harder.
Q503What is loose coupling?
Answer: Loose coupling means components depend on stable abstractions/contracts rather than concrete implementation details.
Q504What is high cohesion?
Answer: High cohesion means a class/module has a focused, closely related set of responsibilities.
Q505What is low cohesion?
Answer: Low cohesion means unrelated responsibilities are grouped together, making a component harder to understand and maintain.
Q506How does OOP help achieve loose coupling?
Answer: Interfaces, abstraction, composition and dependency injection allow clients to depend on contracts instead of concrete implementations.
Q507How does programming to an interface improve design?
Answer: It allows implementations to be replaced without changing clients and improves testing through mocks/stubs.
Q508How does dependency injection relate to OOP?
Answer: DI supplies collaborators from outside a class, reducing direct construction dependencies and supporting polymorphism and SRP.
Q509How does Spring use polymorphism?
Answer: A bean can be injected through an interface or superclass reference while Spring supplies one of several concrete implementations.
Q510How does Spring use interfaces?
Answer: Interfaces define contracts for services, repositories and adapters, allowing interchangeable implementations.
Q511How does Spring achieve dependency injection?
Answer: The container creates and wires beans using constructors, setters or fields; constructor injection is generally preferred.
Q512Where do you use abstraction in Spring Boot?
Answer: Service interfaces, repository abstractions, client interfaces and framework extension points are common examples.
Q513Where do you use encapsulation in Spring Boot?
Answer: Entities, DTOs, services and domain objects encapsulate their state and expose controlled operations.
Q514Where do you use inheritance in Spring Boot?
Answer: Examples include custom exception hierarchies, framework base classes and domain hierarchies, although composition is often preferable.
Q515Where do you use polymorphism in Spring Boot?
Answer: Multiple implementations of a service/client interface are selected using DI, `@Qualifier`, profiles or configuration.
Q516How are interfaces used in Spring Data JPA?
Answer: Repository interfaces define data-access contracts, and Spring Data generates runtime implementations.
Q517How does Spring resolve multiple implementations of an interface?
Answer: It can use `@Primary`, `@Qualifier`, bean names, profiles or explicit configuration to select the desired bean.
Q518How does @Qualifier relate to polymorphism?
Answer: `@Qualifier` selects a particular implementation when several beans implement the same interface.
Q519How does @Primary relate to interface implementations?
Answer: `@Primary` marks one bean as the default candidate when multiple candidates match and no qualifier is specified.
Q520How would you apply SOLID principles to an OOP design?
Answer: Give classes focused responsibilities, extend behavior without unnecessary modification, substitute subtypes safely, keep interfaces focused, and depend on abstractions.

30. Most Frequently Asked Questions to Prioritize

Q521What are the four pillars of OOP?
Answer: Encapsulation hides and controls state; abstraction exposes essential behavior; inheritance enables reuse through an IS-A relationship; polymorphism allows one contract/reference to represent different implementations.
Q522Class vs Object?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q523Object state, behavior and identity?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q524Encapsulation with a real example?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q525Abstraction vs Encapsulation?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q526Abstract class vs Interface?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q527Inheritance and its types?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q528Why doesn't Java support multiple inheritance using classes?
Answer: It avoids ambiguity and complexity such as conflicting inherited implementations and the classic diamond problem.
Q529IS-A vs HAS-A?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q530Composition vs Inheritance?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q531Polymorphism?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q532Compile-time vs Runtime polymorphism?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q533Overloading vs Overriding?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q534Can static methods be overridden?
Answer: No. Static methods are hidden rather than overridden.
Q535Can private methods be overridden?
Answer: No. They are not inherited/visible to the subclass for overriding.
Q536Can final methods be overridden?
Answer: No.
Q537Constructor vs Method?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q538Default vs No-argument constructor?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q539Constructor overloading?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q540Constructor chaining?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q541this() vs super()?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q542this vs super?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q543Access modifiers?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q544Protected access across packages?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q545static vs instance?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q546final variable/method/class?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q547Abstract class restrictions?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q548Upcasting vs Downcasting?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q549instanceof?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q550Dynamic method dispatch?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q551Method hiding?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q552Covariant return type?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q553== vs equals()?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q554equals() and hashCode() contract?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q555Immutable class design?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q556Composition vs Aggregation?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q557Association vs Aggregation vs Composition?
Answer: Explain both concepts, then distinguish them by purpose, scope and runtime/compile-time behavior. In an interview, include a short Java example and mention the most important restriction or rule.
Q558How does Spring Boot use OOP concepts?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q559How does dependency injection demonstrate polymorphism?
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Q560Explain a real-world project example using all four OOP pillars.
Answer: Answer this by stating the Java rule, explaining why it exists, and giving a short code example.
Senior Java interview focus: Be able to connect OOP concepts to Spring Boot: interfaces + dependency injection for polymorphism, encapsulated domain state, composition for services/components, and abstraction through service/repository/client contracts.