Interview questions & answers

OOP (Object-Oriented Programming) interview questions & answers

Object-oriented programming is a paradigm that models software as objects bundling data (attributes) and behavior (methods), organized into classes. Its four core pillars are encapsulation, abstraction, inheritance, and polymorphism, which together promote reusable, maintainable, and modular code. Interviews test whether you can apply these principles to design clean, extensible class hierarchies.

Updated 2026-06-18 · 15 real, commonly-asked questions with answers.

Key takeaways

  • Object-oriented programming is a paradigm that models software as objects bundling data (attributes) and behavior (methods), organized into classes.
  • Core areas to revise for OOP (Object-Oriented Programming): Encapsulation, Abstraction, Inheritance, Polymorphism, Classes & objects.
  • This guide answers 15 of the most-asked OOP (Object-Oriented Programming) interview questions — rehearse them in OnJob's free AI mock interview.
EncapsulationAbstractionInheritancePolymorphismClasses & objectsAbstract classes & interfacesSOLID principlesComposition vs inheritance

Top 15 OOP (Object-Oriented Programming) interview questions

Q1.What are the four pillars of OOP?

The four pillars are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation bundles data with the methods that operate on it and hides internal state; abstraction exposes only essential features; inheritance lets a class derive from another; and polymorphism lets one interface serve different underlying types. Together they enable modular, reusable, and maintainable design.

Q2.What is the difference between a class and an object?

A class is a blueprint or template that defines the attributes and methods a type will have. An object is a concrete instance of a class created at runtime, holding its own actual data. For example, Car is a class while a specific red Toyota in memory is an object of that class.

Q3.What is encapsulation?

Encapsulation is bundling data and the methods that act on it into a single unit while restricting direct access to the internal state. It is achieved with access modifiers like private and public, exposing controlled getters and setters. This protects an object's integrity and lets the implementation change without breaking callers.

Q4.What is the difference between abstraction and encapsulation?

Abstraction is about hiding complexity by exposing only the essential interface, focusing on what an object does. Encapsulation is about hiding internal data and protecting it from outside interference, focusing on how the data is stored and accessed. Abstraction is a design-level concept, while encapsulation is its implementation-level enforcement.

Q5.What is inheritance and what problem does it solve?

Inheritance lets a subclass acquire the attributes and methods of a parent class, promoting code reuse and a natural is-a hierarchy. The subclass can add new members or override inherited ones. It reduces duplication but should be used carefully, since deep or inappropriate hierarchies create tight coupling.

Q6.What is polymorphism and what are its types?

Polymorphism lets a single interface represent different underlying forms, so the same call behaves differently based on the actual object. Compile-time polymorphism is achieved through method overloading (same name, different parameters), while runtime polymorphism is achieved through method overriding (a subclass redefining a parent method). It enables flexible, extensible code.

Q7.What is the difference between method overloading and overriding?

Overloading defines multiple methods with the same name but different parameter lists in the same class, resolved at compile time. Overriding redefines an inherited method in a subclass with the same signature, resolved at runtime based on the object's actual type. Overloading is about offering variations; overriding is about specializing behavior.

Q8.What is the difference between an abstract class and an interface?

An abstract class can have both implemented and unimplemented methods and can hold state, but a class can extend only one. An interface traditionally declares method signatures with no state, and a class can implement many. Use an abstract class for a shared base with common code, and an interface to define a contract that unrelated classes can fulfill.

Q9.What is the difference between composition and inheritance?

Inheritance models an is-a relationship where a subclass extends a parent, while composition models a has-a relationship where an object contains other objects. Composition is more flexible because it avoids tight coupling and lets behavior change at runtime by swapping components. The common guidance is to favor composition over inheritance.

Q10.What are access modifiers?

Access modifiers control the visibility of classes, methods, and fields. Common ones are private (accessible only within the class), protected (within the class and its subclasses), and public (accessible everywhere). They enforce encapsulation by limiting how other code can reach an object's members.

Q11.What is a constructor?

A constructor is a special method automatically called when an object is created, used to initialize its state. It typically has the same name as the class and no return type, and it can be overloaded to accept different parameters. If none is defined, many languages provide a default no-argument constructor.

Q12.What is the difference between a static and an instance method?

A static method belongs to the class itself and is called without an object, so it cannot access instance-specific data. An instance method belongs to an object and can read and modify that object's state through its reference. Static methods suit utility functions, while instance methods operate on a particular object's data.

Q13.What is the SOLID principle?

SOLID is five design principles for maintainable OOP: Single Responsibility (a class has one reason to change), Open-Closed (open for extension, closed for modification), Liskov Substitution (subtypes must be substitutable for their base), Interface Segregation (prefer small focused interfaces), and Dependency Inversion (depend on abstractions, not concretions). Following them produces loosely coupled, flexible code.

Q14.What is dynamic (runtime) polymorphism and how is it achieved?

Dynamic polymorphism resolves which method runs at runtime based on the actual object type rather than the reference type. It is achieved through inheritance and method overriding, where a parent reference points to a child object and the overridden child method executes. This is often implemented internally with a virtual method table.

Q15.What is the difference between aggregation and composition?

Both model a has-a relationship, but composition implies strong ownership where the contained object cannot exist without the container, such as a house and its rooms. Aggregation implies a weaker relationship where the contained object can exist independently, such as a department and its employees. The key difference is the lifecycle dependency between the objects.

Free AI mock interview

Practise OOP (Object-Oriented Programming) out loud

Reading answers is step one. Rehearse them in OnJob's free AI mock interview, get instant feedback, then apply to AI-matched jobs in one click.

Create my free profile — free