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 →
- What are the four pillars of object-oriented programming?
Encapsulation (keep data and the code that uses it together, and hide the inside), abstraction (show only what matters), inheritance (reuse and extend a parent class) and polymorphism (one interface, many behaviours).
BeginnerIn-depth2 min read - What is a constructor, and what is constructor chaining?
A constructor is a special method with the class's name and no return type that runs when an object is created, to set its starting state. Constructor chaining is one constructor calling another, either in the same class with this(...) or in the parent class with super(...), so setup code is written once.
BeginnerIn-depth3 min read - What is encapsulation, and how do access modifiers help achieve it?
Encapsulation means keeping an object's data and the methods that change it together, and hiding the data so outside code can only change it through controlled methods. In Java, access modifiers (private, default, protected, public) decide who can see each member, and making fields private with validated methods is the usual way to encapsulate.
BeginnerIn-depth3 min read - What is the difference between method overloading and method overriding?
Overloading means several methods in the same class share a name but take different parameters, and the compiler picks one. Overriding means a subclass gives its own version of a method it inherited, with the same signature, and the JVM picks the version at runtime based on the actual object.
BeginnerIn-depth3 min read - A class calculates salary, saves it to the database and prints a payslip. How does it break SRP, and how do you fix it?
It has three reasons to change: pay rules, database layout and payslip format. Split it into three classes, each with one job, and let a small coordinator call them.
BeginnerQuick answer1 min read - Does the Single Responsibility Principle mean a class should have only one method?
No. It means a class should have one reason to change, serving one actor or concern. A class can have many methods if they all serve that single purpose.
BeginnerQuick answer1 min read - Explain the Interface Segregation Principle with a Worker interface example.
No class should be forced to implement methods it does not use. Split one big interface into small, focused ones so each class picks only what applies.
BeginnerQuick answer1 min read - Explain the Open/Closed Principle with a payment example.
Classes should be open for extension but closed for modification. To add a new payment method, write a new class that implements an interface instead of editing the checkout code.
BeginnerQuick answer1 min read - How do you achieve abstraction in Java?
Use abstract classes (partial implementations with abstract methods) or interfaces (pure contracts). Callers depend on the abstract type and never see the concrete class behind it.
BeginnerQuick answer1 min read - How do you do dependency injection in JavaScript without any framework?
Pass the dependency as an argument, either to a factory function or a class constructor. A function that receives what it needs is already using dependency injection.
BeginnerQuick answer1 min read - How does dependency injection make unit testing easier?
Because the class receives its collaborators, a test can pass in a fake instead of a real database, network or clock. The test runs fast and gives the same result every time.
BeginnerQuick answer1 min read - Is encapsulation the same as data hiding?
Not exactly. Encapsulation bundles data and the methods that work on it into one unit. Data hiding is the part where the fields are made private so outsiders can only use the safe methods.
BeginnerQuick answer1 min read - Show Dependency Inversion with a NotificationService that currently creates EmailSender inside itself.
Define a Sender interface, have NotificationService depend on it, and pass the implementation in from outside. The service no longer knows whether it is sending email, SMS or WhatsApp.
BeginnerQuick answer1 min read - What are constructor injection and setter injection, and which should you prefer?
Constructor injection passes dependencies when the object is created, so it is never half-built. Setter injection sets them afterwards. Prefer the constructor for required dependencies and setters only for optional ones.
BeginnerQuick answer1 min read - What are the types of inheritance, and which does Java support through classes?
Single, multilevel, hierarchical, multiple and hybrid. Java supports the first three with classes; multiple inheritance of classes is not allowed, but a class can implement many interfaces.
BeginnerQuick answer1 min read - What are the uses of the super keyword?
super refers to the parent part of the current object. It calls the parent constructor, calls a parent method that the child has overridden, or reaches a parent field hidden by a child field.
BeginnerQuick answer1 min read - What does the this keyword mean in Java, and where do you use it?
this is a reference to the current object, the one whose method or constructor is running. It is used to separate fields from parameters of the same name, to call another constructor, and to pass the current object along.
BeginnerQuick answer1 min read - What is duck typing in Python, and how does it relate to polymorphism?
Duck typing means an object is accepted if it has the methods you call, regardless of its class. It gives polymorphism without a shared parent or interface.
BeginnerQuick answer1 min read - What is self in Python class methods, and why must it be written?
self is the first parameter of an instance method and receives the object the method was called on. Python passes it automatically; the name is only a convention.
BeginnerQuick answer1 min read - What is the difference between == and equals() in Java?
== compares references for objects (are they the same object?) and values for primitives. equals() compares content, but only if the class overrides it; the default version behaves like ==.
BeginnerQuick answer1 min read - What is the difference between a class and an object?
A class is a blueprint that describes the fields and methods something will have. An object is one real instance built from that blueprint, with its own values stored in memory.
BeginnerQuick answer1 min read - What is the difference between abstraction and encapsulation?
Abstraction decides what to show: it exposes a simple view and hides complexity. Encapsulation decides how to protect: it bundles data with methods and restricts access to it.
BeginnerQuick answer1 min read - What is the difference between instance and static members of a class?
An instance member belongs to each object and has a separate copy per object. A static member belongs to the class itself, exists once, and is shared by every object.
BeginnerQuick answer1 min read - What is the Observer pattern? Explain with a cricket score example.
A subject keeps a list of observers and tells all of them when its state changes. Observers subscribe and unsubscribe at runtime, and the subject does not need to know what they do with the news.
BeginnerQuick answer1 min read - What is the Singleton pattern and how do you implement it?
Singleton ensures a class has exactly one instance and gives a global access point to it. Make the constructor private, keep the instance in a static field, and return it from a static method.
BeginnerQuick answer1 min read - When does Java provide a default constructor, and when does it not?
If you write no constructor at all, the compiler adds a public no-argument one. The moment you write any constructor yourself, the compiler stops adding it.
BeginnerQuick answer1 min read - 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