Autocoder

OOPs Interview Questions and Answers

Last updated 2026-11-19

Object-oriented programming questions appear in almost every campus and service-company interview, usually before any coding. The risk is giving textbook definitions that collapse under the first follow-up. These answers pair each definition with a small example or a concrete difference so you can survive the next question.

This page covers the whole subject at a fundamental level. For depth, continue to inheritance and polymorphism, abstraction and encapsulation and SOLID principles. If you are preparing in Java, add Java OOPs questions, and for the study plan read how to crack an OOPs interview.

PillarsEncapsulation, abstraction, inheritance, polymorphism
Code inJava, Python and C++
LevelEasy to medium

A reliable answer structure

For any OOPs question, give a one-line definition, one sentence on why it exists, and a two-or-three-line example. Interviewers use the example to decide whether you have used the idea or only read about it.

Keep one running example in mind, such as Vehicle with Car and Bike, or Account with SavingsAccount, and reuse it across answers so your explanations stay consistent.

Differences to have ready

OOPs Interview Questions and Answers: questions with worked answers

  1. 1

    What is OOP and what are its four pillars?

    easy

    Object-oriented programming organises software as objects that combine data (fields) and behaviour (methods). Encapsulation bundles data with the methods that operate on it and restricts direct access. Abstraction exposes what an object does and hides how. Inheritance lets a class reuse and extend another, modelling an is-a relationship. Polymorphism lets one interface take many forms: a Shape reference calls the right area() for a Circle or a Square at run time.

  2. 2

    What is the difference between a class and an object?

    easy

    A class is a blueprint that defines fields and methods; an object is a concrete instance created from it with its own field values, living in memory. Car is a class; the red Swift with plate KA-01 is an object. Many objects can share one class, while the class itself exists once. In Java, new Car() allocates the object on the heap and returns a reference.

  3. 3

    What is a constructor? How is it different from a method?

    easy

    A constructor initialises a new object. It has the same name as the class, no return type, and is called automatically by new. A method has a return type, is called explicitly and can run any number of times. Constructors can be overloaded, and if you declare none, Java and C++ supply a default no-argument constructor. Constructors are not inherited, but a subclass constructor always calls a superclass constructor first.

  4. 4

    Compare method overloading and overriding.

    medium

    Overloading means several methods with the same name but different parameter lists in the same class; the compiler picks one by argument types, so it is compile-time (static) polymorphism. Overriding means a subclass supplies a new body for an inherited method with the same signature; the JVM or runtime picks one based on the actual object type, so it is run-time (dynamic) polymorphism. Return type alone cannot distinguish overloads. An override cannot reduce visibility and may return a covariant type.

    java
    class Printer {
        void print(int x)    { System.out.println("int " + x); }
        void print(String s) { System.out.println("str " + s); }   // overload
    }
    class Animal { String sound() { return "..."; } }
    class Dog extends Animal { @Override String sound() { return "woof"; } }  // override
  5. 5

    Abstract class versus interface: when do you choose each?

    medium

    An abstract class can hold state, constructors and a mix of implemented and abstract methods, and a class may extend only one. An interface declares a contract; since Java 8 it may carry default and static methods, but has no instance state and a class may implement many. Choose an abstract class when subclasses share state or code and are closely related, as in Shape with a name field. Choose an interface when unrelated classes must offer the same capability, such as Comparable or Runnable. See the abstraction page.

  6. 6

    What are association, aggregation and composition? Describe the class diagram.

    medium

    Association is a general "uses" link: a Doctor treats Patients, drawn as a plain line. Aggregation is a weak has-a: a Department has Professors, but professors exist without the department, drawn with an empty diamond on the owner side. Composition is a strong has-a with owned life cycle: a House has Rooms, and rooms are destroyed with the house, drawn with a filled diamond. In code, composition usually means the owner creates and holds the part, while aggregation receives existing objects through a constructor or setter.

    java
    class Engine { }
    class Car {
        private final Engine engine = new Engine();   // composition: Car owns Engine
    }
    class Team {
        private final List<Player> players;            // aggregation: players exist elsewhere
        Team(List<Player> players) { this.players = players; }
    }
  7. 7

    Inheritance or composition: which should you prefer and why?

    medium

    Prefer composition by default. Inheritance couples a subclass to its parent's implementation, so changes in the parent can break children, and it fixes the relationship at compile time. Composition holds a collaborator in a field and delegates to it, so behaviour can be swapped at run time and the pieces tested separately. Use inheritance only for a genuine is-a relationship where the subclass honours the parent's contract. A Stack that extends ArrayList exposes methods like add(index, item) that break stack semantics; a Stack that wraps a list does not.

  8. 8

    What is the diamond problem and how do languages handle it?

    medium

    If D inherits from B and C, and both inherit a method from A, a call on D may be ambiguous and A's data might be duplicated. C++ allows multiple inheritance and solves duplication with virtual inheritance, so one shared A subobject exists. Java forbids multiple inheritance of classes; with interfaces, if two default methods clash the class must override and may call X.super.method(). Python uses the C3 linearisation to build a single method resolution order, visible as D.__mro__.

  9. 9

    How does run-time polymorphism work internally?

    medium

    In C++ a class with virtual functions has a hidden table of function pointers, the vtable, and each object carries a pointer to its class's vtable. A call through a base pointer looks up the function in that table, so the derived override runs. Java does the same conceptually: invokevirtual resolves the method against the object's actual class at run time. Static, private and final methods are bound at compile time and cost nothing extra.

  10. 10

    What do static and final mean?

    easy

    static members belong to the class, not to any instance: one shared copy, accessible without an object, and a static method cannot use this or instance fields directly. In Java, final on a variable makes it assignable once, on a method prevents overriding, and on a class prevents subclassing. A static final field is a constant. Remember that a final reference does not make the object it points to immutable.

  11. 11

    Explain access modifiers in Java.

    medium

    private: visible only inside the class. Default (package-private): visible within the same package. protected: the same package plus subclasses in any package. public: everywhere. Use the narrowest level that works, since smaller visibility means fewer places that depend on the member. In C++ private, protected and public also control inheritance mode, and friend functions can bypass them.

  12. 12

    What are coupling and cohesion?

    medium

    Cohesion measures how closely a class's responsibilities belong together; high cohesion means a class does one job, like InvoicePrinter. Coupling measures how much one class depends on others; low coupling means changing one class rarely forces changes in another. Good design aims for high cohesion and low coupling, achieved by programming to interfaces, injecting dependencies and keeping classes small. A god class with fifteen unrelated methods is low cohesion; a class that reaches into another's fields is tightly coupled.

  13. 13

    What is object slicing in C++, and how do you avoid it?

    hard

    Assigning or passing a derived object by value to a base type copies only the base part, discarding derived members and the derived vtable pointer, so virtual calls on the copy run the base version. It happens with void f(Base b) called with a Derived. Avoid it by passing by reference or pointer, or smart pointer, and by making the base class abstract or deleting its copy operations. Always give polymorphic base classes a virtual destructor.

    cpp
    struct Base { virtual const char* name() const { return "Base"; } virtual ~Base() = default; };
    struct Derived : Base { const char* name() const override { return "Derived"; } };
    void byValue(Base b)       { puts(b.name()); }   // prints Base (sliced)
    void byRef(const Base& b)  { puts(b.name()); }   // prints Derived
  14. 14

    How do you copy objects: shallow versus deep copy?

    medium

    A shallow copy duplicates the object's fields, so reference fields in the copy point at the same nested objects as the original; changing a nested list through one affects the other. A deep copy duplicates the nested objects too, giving an independent graph. In Python, copy.copy is shallow and copy.deepcopy is deep; in Java, clone() is shallow by default, and copy constructors or serialization give deep copies. For immutable fields sharing is safe.

    python
    import copy
    a = [[1, 2], [3]]
    s = copy.copy(a); d = copy.deepcopy(a)
    a[0].append(9)
    print(s[0], d[0])   # [1, 2, 9] [1, 2]
  15. 15

    Design a small class hierarchy for payments and show where polymorphism pays off.

    hard

    Define an abstract Payment interface with pay(amount), and classes CardPayment, UpiPayment and WalletPayment implementing it. A Checkout class holds a Payment reference and calls pay(), never branching on type. Adding NetBanking means adding one class, not editing Checkout, which is the open/closed principle at work. Trade-off: if payment types differ widely in required inputs, a single interface can become a lowest-common-denominator, so pass a PaymentRequest object. A fuller walk-through is in low-level design questions.

Frequently asked questions

Is Python fully object-oriented?

Everything in Python is an object, and it supports classes, inheritance and polymorphism. It lacks strict access control; privacy is a convention using underscores.

Is Java 100 percent object oriented?

Not strictly, because primitive types such as int are not objects, and static members belong to classes. People usually say it is mostly object oriented.

Which OOP concept is asked most often?

Polymorphism and the difference between abstract classes and interfaces come up most, followed by SOLID in interviews for experienced candidates.

Should I learn OOPs in Java or C++?

Use the language you code in. Concepts transfer, but know C++ specifics such as virtual destructors if the company uses C++.

Related guides

More in CS Fundamentals Interview Questions

See all in this section