Core CS · 17 questions

Object-Oriented Programming interview questions

The four pillars, overloading and overriding, abstract classes and interfaces, inheritance and composition, SOLID: the OOP questions every campus interview asks, with Java and C++ notes.

By Akshay, software engineer, India

Flashcards for these

The four pillars

What are the four pillars of OOP?

  • Encapsulation: keep data and the methods that use it together, and hide the data (private fields, public methods). A BankAccount lets you deposit(), not set balance directly.
  • Abstraction: show what something does, hide how. You call list.sort() without knowing the algorithm.
  • Inheritance: a class builds on another (SavingsAccount extends Account) and reuses its code.
  • Polymorphism: one interface, many forms: shape.area() runs the right code for a circle or a square.

Abstraction vs encapsulation?

Abstraction is a design idea: decide what to show (the interface). Encapsulation is how you enforce it: hide the internals behind access modifiers. Abstraction answers "what does it do?"; encapsulation protects "how it does it" from outside changes. A car's pedals are the abstraction; the bonnet you don't open is the encapsulation.

Class vs object?

A class is the blueprint: the fields and methods. An object is one instance of it, with its own values, in memory. Student is a class; new Student("Asha") is an object. A class takes no memory for data until objects are made (static fields aside).

Polymorphism

Overloading vs overriding?

Overloading: the same method name with different parameters in one class (add(int, int), add(double, double)). Chosen at compile time: compile-time (static) polymorphism.

Overriding: a subclass gives its own version of a parent method with the same signature. Chosen at run time from the object's real type: run-time (dynamic) polymorphism.

How does run-time polymorphism work underneath?

With a virtual table. Each class with virtual methods has a table of pointers to its versions of those methods, and each object holds a pointer to its class's table. Calling shape->area() looks up the method in the object's table, so a Circle runs Circle::area even through a Shape*. In C++ only virtual methods work this way; in Java every non-static, non-final, non-private method does.

Classes, interfaces and inheritance

Abstract class vs interface?

An abstract class can have state (fields), constructors and implemented methods along with abstract ones; a class extends only one. An interface is a contract: what a class can do. A class can implement many. Since Java 8, interfaces can have default and static methods, but still no instance fields.

Rule of thumb: an abstract class for "is a" with shared code (Account); an interface for "can do" (Comparable, Runnable).

What are the types of inheritance, and what is the diamond problem?

Single, multilevel (A → B → C), hierarchical (B and C both extend A), multiple (C extends A and B) and hybrid.

The diamond problem: B and C both extend A and override a method; D extends both B and C. Which version does D get, and does it hold one copy of A or two? Java forbids multiple inheritance of classes (interfaces are allowed, and a conflicting default method must be overridden). C++ allows it and solves the duplicate-A part with virtual inheritance.

Composition vs inheritance?

Inheritance is "is a": a Car is a Vehicle. Composition is "has a": a Car has an Engine. Prefer composition: it's looser (you can swap the engine), it avoids deep, fragile hierarchies, and a change in a parent class can't break the child unexpectedly. Use inheritance when the "is a" is real and stable.

Association vs aggregation vs composition?

All three are relationships between objects. Association: they know each other (a teacher and a student). Aggregation: has-a, but the part can live on its own (a department has teachers; close the department and the teachers remain). Composition: has-a where the part dies with the whole (a house and its rooms).

Constructors and members

What are constructors, and can one be private?

A constructor sets up a new object: same name as the class, no return type. Types: default (no parameters), parameterised, and in C++ the copy constructor. Constructors can be overloaded, not inherited.

Yes, a constructor can be private: then only the class itself can create objects. That's how a singleton (one instance, reached through getInstance()) or a class of static helpers is built.

What do the access modifiers mean?

private: only inside the class. protected: the class and its subclasses (in Java, also the same package). public: everyone. Java's default (no keyword): the same package only. In C++, class members are private by default and struct members public.

What does static mean?

It belongs to the class, not to any object: one copy shared by all instances. A static method can be called without an object (Math.max), and it can't use this or instance fields directly. Common uses: counters shared by all objects, constants, utility methods and factory methods.

this vs super?

this refers to the current object: to tell a field from a parameter (this.name = name) or to call another constructor of the same class. super refers to the parent part: to call the parent's constructor (it must be the first line in Java) or the parent's version of an overridden method.

Shallow copy vs deep copy?

A shallow copy copies the fields, but objects inside are shared: copy a Student with a list of marks, add to one copy's list, and both change. A deep copy also copies the objects inside, so the two are fully independent. In C++ this is why a class holding a raw pointer needs its own copy constructor.

What does final do in Java?

On a variable: it can be assigned only once (a constant; for an object, the reference can't change, but the object can). On a method: it can't be overridden. On a class: it can't be extended, like String.

Design

What are the SOLID principles?

  • Single responsibility: a class has one reason to change.
  • Open/closed: open for extension, closed for modification (add a new Shape class instead of editing a switch).
  • Liskov substitution: a subclass must work wherever its parent does (the square-and-rectangle example breaks it).
  • Interface segregation: many small interfaces beat one fat one.
  • Dependency inversion: depend on abstractions, not concrete classes (take a PaymentGateway interface, not RazorpayClient).

Name a few design patterns and when you would use them.

  • Singleton: exactly one instance (a config or a connection pool); careful, it's global state.
  • Factory: create objects without naming the exact class (PaymentFactory.create("upi")).
  • Observer: notify subscribers when something changes (events, UI updates).
  • Strategy: swap an algorithm at run time (different discount rules).
  • Builder: build complex objects step by step (an HTTP request with optional parts).
Keep it for the interview

A few cards a day, back just before you'd forget them.

Core CS flashcards