Java OOPs Interview Questions & Answers
This guide provides an exhaustive list of detailed interview questions and answers focused on Java Object-Oriented Programming (OOPs), specifically curated to help experienced developers tackle tricky MNC interview scenarios.
1. Object Creation & Memory Management
Q: What happens internally when you create an object using new? A: Java performs several steps behind the scenes:
- Memory Allocation: Space is created in the Heap memory for the new object.
- Default Values Set: All instance variables are given default values (e.g.,
0for numbers,falsefor booleans,nullfor objects). - Constructor Execution: The constructor runs, applying any passed values and executing setup logic.
- Reference Returned: The memory address of the created object is returned and stored in the reference variable in the Stack.
Q: Can we create an object without using the new keyword? If yes, how? A: Yes, there are several ways: using the clone() method, using the Reflection API (Class.forName().newInstance()), using Deserialization, or using Factory methods (like String.valueOf()).
Q: Can a class be declared without any variables or methods? A: Yes. Java still compiles it into a .class file and provides a default no-argument constructor. We can create objects from it. This is often called a "Marker Class" when used simply to represent a specific type.
Q: What is the difference between an object and an object reference? A: The object is the actual data residing in the Heap memory. The object reference is a variable residing in the Stack memory that holds the memory address pointing to the object.
Q: Does assigning null to a reference delete the object? A: No. Assigning null only severs the connection (the pointer) from the reference variable to the object. The object remains in the Heap as "unreachable" until the Garbage Collector decides to destroy it.
Q: Can a class have only static members? If yes, is it still object-oriented? A: Yes, it can (like java.lang.Math). However, conceptually, it is not purely object-oriented because OOP is about objects possessing state and behavior. Static members belong to the class blueprint itself, making the approach more procedural.
2. Encapsulation & Abstraction
Q: Is encapsulation only about making variables private? A: No. Encapsulation means keeping data safe. It involves making variables private AND controlling how that data is accessed or modified through getters and setters.
Q: If a class has private fields but public setters, is it truly encapsulated? A: Not necessarily. If a setter allows any arbitrary value without checks, the data isn't really protected. True encapsulation requires business logic and validation rules inside the setter (e.g., throwing an error if someone tries to set age = -5).
Q: Why do we use getters/setters when we can make fields public and access them directly? A: Because encapsulation is about hiding internal representations. Getters and setters allow you to inject validation, make fields read-only (by omitting the setter), and change internal logic later without breaking external code that uses the class.
Q: What are anonymous classes? A: An anonymous class is a class without a name that is declared and instantiated at the exact same time. It is used when you need a small, custom implementation of an interface or class for a one-time use (like a button click listener) without creating a separate .java file.
Q: What is the difference between encapsulation and abstraction? A: Encapsulation hides data to protect it (like a pill capsule protecting the medicine inside). Abstraction hides internal complexity to make the system easier to use (like a TV remote showing simple buttons while hiding the complex circuit board).
Q: What problem does abstraction solve that encapsulation does not? A: Encapsulation solely protects data integrity. Abstraction reduces cognitive complexity for the user. It allows developers to interact with high-level concepts without needing to understand the underlying, low-level operational details.
3. Abstract Classes & Interfaces
Q: Why do we need abstract classes when we already have interfaces? A: Abstract classes are used when classes are closely related and need to share actual state (instance variables, constructors) or common method implementations. Interfaces are used when unrelated classes need to abide by a common contract but share no inherent state.
Q: Can a class exist without any abstract methods? A: Yes. An abstract class without abstract methods simply means the class cannot be directly instantiated and is meant strictly to be inherited by subclasses.
Q: Why can an interface not have instance variables? A: An interface represents a contract, not an object blueprint. Instance variables belong to an object's state. Because you cannot instantiate an interface, it cannot hold state. Therefore, all variables in an interface are implicitly public static final.
Q: Why were default, static, and private methods introduced in interfaces after Java 8? A: * default methods: Allow adding new methods to an interface without breaking existing classes that implement it.
staticmethods: Serve as helper/utility methods tightly coupled to the interface.privatemethods: Added in Java 9 to allowdefaultmethods to share common logic without exposing that logic to implementing classes.
Q: Why does Java support multiple inheritance using interfaces but not classes? A: Classes do not support multiple inheritance to avoid the "Diamond Problem" (if two parent classes have the same method, the child doesn't know which one to inherit). Interfaces avoid this because they primarily define method signatures without bodies; the child class is forced to provide the single concrete implementation itself, eliminating ambiguity.
Q: Can an interface exist without any methods? A: Yes. This is called a Marker Interface (like Serializable or Cloneable). It marks a class to signal special treatment to the JVM or a framework.
Q: What exactly makes an interface a functional interface? A: An interface is functional if it contains exactly one abstract method. This allows it to be used seamlessly with Lambda expressions.
Q: Can a functional interface have default and static methods? A: Yes. Default and static methods have implementations, so they do not count towards the "one abstract method" limit.
Q: Why is the @FunctionalInterface annotation optional but recommended? A: The compiler automatically detects if an interface fits the rule. However, using the annotation clearly communicates your design intent to other developers and causes a compile-time error if someone accidentally adds a second abstract method, breaking the lambda capability.
Q: Can a functional interface extend another interface? A: Yes, but only if the combined total of abstract methods remains exactly one. If the parent has one abstract method and the child adds another, it ceases to be a functional interface.
4. Overriding, Overloading, & Methods
Q: Why can't private methods be overridden? A: Private methods are restricted to the class they are declared in. Subclasses cannot even "see" them. Since overriding requires visibility of the parent method, private methods cannot be overridden.
Q: Why can't static methods be overridden and only hidden? A: Overriding relies on dynamic polymorphism (resolved at runtime based on the actual object). Static methods belong to the class and are resolved at compile-time based on the reference type. If a subclass writes a static method with the same signature, it "hides" the parent method rather than overriding it.
Q: What is a covariant return type and why is it allowed? A: It allows an overridden method in a subclass to return a more specific type than the parent method (e.g., parent returns Vehicle, child returns Car). It is allowed because a Car is still a Vehicle, so it fulfills the original method's contract perfectly.
Q: Can we override the main method? A: No. The main method is static, and static methods cannot be overridden.
Q: Why is overloading resolved at compile time but overriding at runtime? A: Overloading is determined by the method signature (name and parameters). The compiler looks at the arguments you pass and binds the call instantly. Overriding depends on the actual object instantiated in memory at runtime, forcing the JVM to decide dynamically.
Q: Can you overload a method by only changing the return type? A: No. The compiler distinguishes methods by their name and parameter list. If only the return type differs, the compiler throws an error because it cannot figure out which method to invoke.
Q: Can you overload static methods? A: Yes, you can have multiple static methods with the same name as long as their parameter lists are different.
Q: Can you overload the main method? A: Yes, by changing its parameters. However, the JVM will strictly only look for the standard public static void main(String[] args) to launch the application.
Q: Can you overload private methods? A: Yes. Overloading happens within the same class context and has nothing to do with access modifiers or inheritance.
5. Constructors
Q: Can a constructor be overridden? A: No. Constructors are not inherited by subclasses. Since you can't inherit them, you cannot override them.
Q: Can an abstract class have a constructor? A: Yes. Even though you cannot instantiate an abstract class directly, its constructor is called via super() when a concrete subclass is instantiated to set up the inherited state.
Q: Why doesn't Java allow static constructors? A: Constructors are designed to build and initialize object instances. Static members belong to the class itself. Therefore, a static constructor contradicts the very purpose of an object builder. (Static initialization blocks are used instead).
Q: Can a constructor call another constructor using this() and super()? A: A constructor can use this() to call a sibling constructor or super() to call a parent constructor. However, Java requires that this call be the very first statement. Therefore, you cannot use both in the exact same constructor.
Q: Why are constructors not inherited but accessible via super()? A: Constructors are uniquely tied to their specific class name to build that specific object. However, a child object physically contains the parent object's state, so it uses super() to ensure the parent's constructor runs and initializes that inherited state.
Q: What happens if you make all constructors private? A: No outside class can use the new keyword to create an object of that class. This is standard practice in the Singleton design pattern.
Q: What problems arise with too many constructors? A: It creates confusion for developers using the API, clutters the code, and often leads to duplicated initialization logic which breeds bugs.
6. Access Modifiers & Keywords
Q: Can a class in another package access protected members via object references? A: No. A subclass in a different package can only access protected members via inheritance (using them directly or via super). It cannot create an instance of the parent class and access the protected member using the dot operator.
Q: Why are interface methods always public even if not specified? A: Interfaces exist to define a public-facing contract that classes across entirely different packages can implement. If they were anything but public, wide implementation would be impossible.
Q: What happens if a class has no access modifier in a multi-module project? A: It defaults to "package-private" (visible only within its exact package). In multi-module projects, packages usually span different boundaries, meaning the class will be completely inaccessible to other modules.
Q: Why can a top-level class not be private or protected? A: A top-level class must be accessible to something to be useful. If it were private, nothing outside its own file could see it. Protected implies subclass-based access across packages, which doesn't make sense for a file's root class. It must be public or package-private.
Q: Can fields be public but still maintain encapsulation? A: Generally, no. However, if a field is public, final, and holds an immutable object (like a String constant), it is safe because its value is locked and cannot be mutated maliciously.
Q: What is the this and super keyword? A: this refers to the current executing object (useful for distinguishing instance variables from method parameters). super refers to the parent class object (useful for invoking overridden parent methods or variables).
Q: What happens if you use this() and super() together in a constructor? A: The compiler throws an error. Both commands demand to be the absolute first line of execution in the constructor, which is impossible.
Q: Can this or super be used inside a static method? A: No. Static methods execute at the class level without any object instance. Since this and super require an active object reference, they are invalid in static contexts.
Q: Can super access private members of a parent class? A: No. Private strictly limits visibility to the defining class, overriding even super calls from a subclass.
Q: Can this and super be used together in the same method? Give a scenario. A: Yes, in instance methods. Example: A SportsCar has an overridden getSpeed() method. Inside SportsCar, you could write logic comparing its own speed (this.getSpeed()) against a standard car's speed (super.getSpeed()).
7. Design Principles: Cohesion, Coupling, & Relationships
Q: What is the difference between Cohesion and Coupling? A: * Cohesion: Measures how strongly related the responsibilities of a single class are. (High cohesion = the class does exactly one logical thing well).
- Coupling: Measures how heavily one class relies on other classes. (Low coupling = classes are independent and swapping one won't break others).
Q: Can a class have high cohesion but still be tightly coupled? Give a real example. A: Yes. A PaymentProcessor class might only handle payments (High Cohesion). However, if inside its methods it uses new StripeAPI(), new MySQLDB(), and new SMSNotifier(), it is tightly coupled to those concrete classes. If the database changes, the PaymentProcessor breaks.
Q: Is it possible to increase cohesion and coupling at the same time? A: Yes. If you take a massive "God Class" and break it into five smaller, highly focused classes, you increase cohesion. However, because those five classes must now pass data to one another to complete the original workflow, coupling between them increases.
Q: What is the difference between Association, Aggregation, and Composition? A: * Association: A general connection where objects know each other but have independent lifecycles (Doctor and Patient).
- Aggregation: A "has-a" relationship representing a weak whole/part dynamic. The part can survive without the whole (School and Teacher).
- Composition: A strong "has-a" relationship representing strict ownership. The part dies if the whole dies (House and Room).
Q: Can aggregation exist without association? A: No. Aggregation is simply a specialized, stricter category of Association. Without the basic link (Association), the concept of a whole/part dynamic cannot exist.
Q: What design problem can occur if you use composition everywhere? A: The system becomes rigid, brittle, and highly coupled. If a parent object is modified or destroyed, everything beneath it cascades into destruction. It destroys code reusability.
Q: Give a real-world example where aggregation is a better choice than composition. A: An Airport and an Airplane. An airport "has" airplanes. If the airport is permanently closed and demolished, the airplanes do not get destroyed along with it; they simply fly to a different airport. This is aggregation.