OOP · Trade-off
Shape: abstract class or interface?
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
You're writing a drawing app with circles, rectangles and triangles. Every shape has a colour and an area. Would you make Shape an abstract class or an interface, and why? What if some shapes also need to be printable and resizable?
Then
Follow-ups the interviewer asks next
- What can an abstract class have that an interface can't?
- Since Java 8, interfaces can have default methods. So what's the difference now?
- Can a class extend two abstract classes? Implement two interfaces?
- How do you make an interface in C++?
- Can an abstract class have a constructor? Why would it?
The answer
What a strong answer covers
- Abstract class: can hold fields (state), constructors and normal methods along with abstract ones; a class can extend only one.
- Interface: a contract of what a class can do; a class can implement many. Fields are public static final; methods are public and abstract unless they are default or static (Java 8) or private (Java 9).
- Shape as an abstract class: every shape shares a colour field and maybe a describe() method, and
area()is abstract. - Printable and Resizable as interfaces: unrelated things (a report, an image) can be printable too, and one class can take several.
- Rule of thumb: "is a" with shared code → abstract class; "can do" ability → interface.
- C++ has no interface keyword: a class with only pure virtual functions, like
virtual double area() const = 0;, plus a virtual destructor. - An abstract class's constructor runs when a subclass object is created and sets shared fields such as colour.
The red pen
Common traps
Avoid:"interfaces can't have method bodies" (out of date since Java 8); "abstract classes can't have constructors"; picking one without a reason.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.