Object-Oriented Programming

  • Writing Code
  • 12 questions

What this round is for

OOP questions look like vocabulary questions and are not. Anybody can recite four pillars; the follow-up is always "give me an example from something you built", and that is the part being marked.

So learn each idea with one small example you have actually written, and one case where you would not use it. An answer that includes "and here is when I would not" is the single fastest way to sound like somebody who has shipped code.

The questions

Answer each one out loud before you open it. An answer you have read is not an answer you can give under pressure, which is the only kind that counts.

Q1 What is the difference between a class and an object?

Show the answer Hide the answer

A class is the definition; an object is one thing made from it, with its own data.

The class says what fields and methods exist. The object is a particular set of values for those fields, living in memory. One class, many objects, each with its own state - which is why changing one object does not change another.

What it is really testing. That you can talk about a program at runtime, not only as source code. The giveaway answer is one that never mentions state or memory.

Q2 What are the four pillars of OOP?

Show the answer Hide the answer

Encapsulation, abstraction, inheritance and polymorphism.

Encapsulation keeps data and the code that changes it together, and keeps everything else out. Abstraction exposes what a thing does and hides how. Inheritance lets one type reuse another. Polymorphism lets one call site work with several types.

What it is really testing. Nothing on its own - this is the warm-up. The real question is the next one, so have an example ready for each before you are asked.

Q3 What is the difference between abstraction and encapsulation?

Show the answer Hide the answer

Abstraction is about the interface you show; encapsulation is about the data you hide.

They are often answered as if they were the same thing. Abstraction is a design decision - a "Payment" type whose users never learn which provider is behind it. Encapsulation is a mechanism - the balance field is private, so the only way to change it is a method that can enforce the rules.

What it is really testing. Whether you have thought about either, or only memorised both. This is the most commonly muddled pair in the subject, and interviewers know it.

Q4 What is the difference between method overloading and overriding?

Show the answer Hide the answer

Overloading is two methods with the same name and different parameters in one class; overriding is a subclass replacing a method it inherited.

Overloading is resolved from the arguments, usually when the code is compiled. Overriding is resolved from the actual type of the object at run time, which is the mechanism polymorphism is built on.

What it is really testing. Compile time versus run time. If you can attach those two words to the right one, you have answered the question behind the question.

Q5 What is polymorphism? Give an example.

Show the answer Hide the answer

One call site working with several types, each doing its own thing.

A function that takes a Shape and calls area() does not know or care whether it has a Circle or a Square; each supplies its own area(). The value is that adding a Triangle later changes no existing code - which is the point, not the vocabulary.

What it is really testing. Whether you can name the benefit. Plenty of candidates define the word correctly and cannot say why anybody would want it.

Q6 What is the difference between an abstract class and an interface?

Show the answer Hide the answer

An abstract class can carry state and partly-written methods; an interface is a promise about what exists, with no state.

Use an abstract class when the subtypes genuinely share implementation - a base that already does half the work. Use an interface when unrelated types need to be usable in the same place. Most languages allow many interfaces and only one base class, which is often what decides it.

What it is really testing. Whether you can choose between them, not only list the differences. Expect "which would you use here?" immediately afterwards.

Q7 What is inheritance, and when should you not use it?

Show the answer Hide the answer

It is one type taking on another type's behaviour, and you should not use it merely to share code.

Inheritance says "is a". A Manager is an Employee, so that works. Sharing a few helper methods is not "is a" - that is what composition is for. The classic failure is a deep hierarchy built to avoid duplication, where changing the base breaks four subclasses nobody remembered.

What it is really testing. Judgement. Anybody can define inheritance; the question is whether you have been bitten by it, and the "when not to" is where that shows.

Q8 What does "composition over inheritance" mean?

Show the answer Hide the answer

Prefer building a type out of other objects it holds, rather than out of a parent it extends.

A Car that has an Engine can take a different engine later, and the Engine can be tested alone. A Car that extends Engine can do neither, and inherits every method Engine ever grows. Composition keeps the pieces replaceable, which is usually what you actually wanted.

What it is really testing. Whether you have read past the textbook. This phrase appears in every style guide and in very few first-year courses.

Q9 What is the difference between == and equals()?

Show the answer Hide the answer

One usually compares identity - are these the same object - and the other compares value.

The exact split depends on the language, which is itself the right thing to say. In Java == on objects compares references and equals() compares value. In JavaScript == coerces types and === does not. In Python == compares value and "is" compares identity. Name your language, then answer.

What it is really testing. Whether you know which language you are in. Answering with Java rules during a Python interview is a worse signal than saying "which language?"

Q10 What is a constructor? Can it be private?

Show the answer Hide the answer

It is the code that runs when an object is created, and yes, it can be private.

A private constructor stops callers creating the object directly, which is how singletons and factory methods are built - the class controls its own creation. A static create() that validates its input before constructing is the everyday use, and a more useful answer than the singleton.

What it is really testing. Whether "can it be private" surprises you. It is a small trapdoor question that separates recitation from use.

Q11 What is the Single Responsibility Principle?

Show the answer Hide the answer

A class should have one reason to change.

Note the wording: one reason to change, not one method or one job. A class that formats a report and emails it has two reasons - the format changes, and the mail provider changes - and those two will be asked for by different people at different times. That is what makes it a problem.

What it is really testing. Precision. "It should do one thing" is the common answer and is vague enough to be useless; "one reason to change" is the actual formulation and tells you where to cut.

Q12 What is a static method, and when is using one a mistake?

Show the answer Hide the answer

It belongs to the class rather than to any object, and it is a mistake when it needs to be swapped out in a test.

Static is right for genuinely stateless helpers - Math.max. It goes wrong as soon as it reaches out to a database, a clock or the network, because the caller now has no way to substitute it. That is the usual cause of "this class cannot be unit tested".

What it is really testing. Whether you have written tests. The testability answer is one a candidate only gives after being stuck behind a static call.

Two things worth doing in the room

  • Have one real example per pillar, from code you wrote. "In my project the Report class was abstract because..." beats any textbook definition.
  • When asked to define something, define it and then immediately say when it is the wrong choice. That second half is what interviewers write down.