Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- Why is creating dependencies with new inside a class considered a problem?
It hard-wires the class to one concrete implementation, hides what it depends on, and makes it impossible to replace in tests or in other environments without editing the class.
BeginnerQuick answer1 min read - Why use a factory instead of calling new directly?
A factory hides which concrete class is created and how. Callers ask for what they need, and creation rules can change in one place without touching the callers.
BeginnerQuick answer1 min read - Why use getters and setters when we could make fields public?
Methods let a class validate input, compute values, log changes, and change its internal storage later without breaking callers. A public field gives up all of that control.
BeginnerQuick answer1 min read - Are constructors and private members inherited in Java?
Constructors are never inherited; a child must define its own and call the parent's with super(). Private members exist inside the child object but cannot be accessed by name from the child.
IntermediateQuick answer1 min read - Can an abstract class have a constructor? Why would it need one?
Yes. It cannot be instantiated directly, but a subclass constructor calls it with super() so the parent's fields get initialised.
IntermediateQuick answer1 min read - Can static methods be overridden in Java?
No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.
IntermediateQuick answer1 min read - Does Python have private attributes? What do _x and __x mean?
Python has no true private. A single underscore _x is a hint that it is internal. A double underscore __x triggers name mangling, which renames it to _ClassName__x to avoid clashes in subclasses.
IntermediateQuick answer1 min read - Does the Open/Closed Principle mean you can never edit an existing class?
No. You still fix bugs and change requirements. OCP is about designing so that common kinds of change, such as adding a new variant, need new code instead of edits to old code.
IntermediateQuick answer1 min read - How can polymorphism replace a long if-else or switch chain?
Give each case its own class with the same method, then call that method on the object. Adding a new case means adding a class, not editing the old, tested chain.
IntermediateQuick answer1 min read - How do you create an immutable class in Java?
Make the class final, all fields private and final, set them only in the constructor, add no setters, and copy any mutable object on the way in and on the way out.
IntermediateQuick answer1 min read - How do you make a property private in JavaScript classes?
Prefix the field with
IntermediateQuick answer1 min read - How do you make a Singleton thread-safe in Java?
Use eager initialisation, the holder class idiom, or an enum. If you need lazy creation with double-checked locking, the field must be volatile.
IntermediateQuick answer1 min read - How do you spot a Single Responsibility violation in real code?
Look for a class whose description needs the word and, imports from unrelated areas, unrelated groups of fields, or edits from different people for different reasons.
IntermediateQuick answer1 min read - How is the Interface Segregation Principle different from the Single Responsibility Principle?
SRP is about a class having one reason to change. ISP is about the interface a client sees: it should be small and fit that client's needs. One is about implementation, the other about the contract.
IntermediateQuick answer1 min read - How would you build a simple factory in Python without a long if-elif chain?
Keep a dictionary that maps names to classes and look the class up. Adding a new type is a one-line registration rather than another elif branch.
IntermediateQuick answer1 min read - Show the Open/Closed Principle in JavaScript with a list of validation rules.
Keep rules as a list of small functions and run them all with one loop. A new rule is added by appending a function, and the validating code is never changed.
IntermediateQuick answer1 min read - What are the signs that code violates the Liskov Substitution Principle?
Subclasses that throw UnsupportedOperationException, override a method to do nothing, or callers that must check instanceof before using a parent-typed object.
IntermediateQuick answer1 min read - What does an IoC container do, and when is it worth using?
It creates objects, works out their dependencies and injects them for you, often also controlling their lifetime. It saves manual wiring in large applications, but is unnecessary in small ones.
IntermediateQuick answer1 min read - What happens in memory when you write new Student("Asha") in Java?
new allocates memory for the object on the heap, sets fields to default values, runs the constructor, and returns a reference. The variable on the stack holds only that reference.
IntermediateQuick answer1 min read - What is dynamic method dispatch in Java?
It is the way Java chooses which overridden method to run at runtime, based on the actual object type and not on the type of the reference variable.
IntermediateQuick answer1 min read - What is the difference between a shallow copy and a deep copy of an object?
A shallow copy duplicates the object but shares the objects it refers to. A deep copy duplicates the whole graph, so the copy shares nothing with the original.
IntermediateQuick answer1 min read - What is the difference between Dependency Inversion, Dependency Injection and Inversion of Control?
DIP is a design rule: depend on abstractions. DI is a technique: pass dependencies in from outside. IoC is the wider idea that a framework or caller controls the flow instead of your code.
IntermediateQuick answer1 min read - What is the difference between protected and default (package-private) access in Java?
Default access lets only classes in the same package use the member. Protected allows the same package plus subclasses in any package, but through inheritance.
IntermediateQuick answer1 min read - What is the difference between the Factory Method and Abstract Factory patterns?
Factory Method lets a subclass decide which single product to create. Abstract Factory creates a whole family of related products that are meant to be used together.
IntermediateQuick answer1 min read - What is the difference between the Observer pattern and Publish-Subscribe?
In Observer, observers register directly with the subject and the subject calls them. In Pub-Sub, a message broker sits between publishers and subscribers, so neither knows the other.
IntermediateQuick answer1 min read - When does an object become eligible for garbage collection in Java?
An object becomes eligible when no live thread can reach it through any chain of references. The JVM then frees its memory at a time of its own choosing.
IntermediateQuick answer1 min read - Why do many developers call Singleton an anti-pattern?
It is global state in disguise: hidden dependencies, hard-to-reset state between tests, tight coupling to one concrete class, and trouble when you later need two instances.
IntermediateQuick answer1 min read - Why does Java not allow a class to extend two classes?
To avoid the diamond problem: if two parents define the same method and a child inherits both, it is unclear which version should run. Java blocks it and lets you combine interfaces instead.
IntermediateQuick answer1 min read - Why does Square extending Rectangle violate the Liskov Substitution Principle?
A Square must keep width and height equal, so setting one changes the other. Code written for any Rectangle, expecting independent sides, gets wrong results when given a Square.
IntermediateQuick answer1 min read - Why must you unsubscribe observers, and how do you design for it?
A subject holds strong references to its observers, so a forgotten one stays in memory and keeps receiving events. Return an unsubscribe function from subscribe() and call it when done.
IntermediateQuick answer1 min read