Interfaces vs abstract classes

OOP ยท 8 interview questions

An interface declares a contract with no state. An abstract class is a partially implemented class that can hold fields and constructors but cannot be instantiated.

The traditional rule was that interfaces have no implementation and abstract classes do. Modern languages have blurred this: Java 8 added default methods on interfaces, and C# followed with default interface implementations, so interfaces can now carry behaviour too.

The distinction that survives is state and identity. Interfaces still cannot hold instance fields, and a class can implement many but extend only one. So the question becomes: are you sharing a contract, or sharing state and a construction path?

Interfaces vs abstract classes interview questions

What's the core difference between an interface and an abstract class?
An abstract class can hold instance state and constructors and can only be extended once. An interface declares a contract without instance state, and a class can implement many of them.
Can an interface contain method implementations?
In modern Java and C#, yes โ€” default methods let an interface supply a body, mainly so an interface can gain a method without breaking every existing implementer. It still cannot hold instance fields.
Why they ask: Says whether a candidate's knowledge is current or stopped at Java 7.
When would you choose an abstract class over an interface?
When implementations must share state or constructor logic, when you want a protected method not visible to callers, or when the types genuinely form one family that should not extend anything else.
When would you choose an interface?
When unrelated classes need the same capability, when a class must play several roles at once, or when you only want to specify a contract and leave everything about implementation open.
Why can a class implement many interfaces but extend only one class?
Because interfaces carry no instance state, so combining them raises no question about which copy of a field an object holds. Multiple class inheritance would, which is the ambiguity the restriction avoids.
What is a marker interface?
An interface with no methods, used purely to tag a type so code can check for it โ€” Java's Serializable and Cloneable are examples. Annotations and attributes have largely replaced the pattern.
Can a class extend a class and implement interfaces at the same time?
Yes, and it's common. One parent class for shared state and behaviour, plus any number of interfaces for the roles it fulfils.
What happens if two interfaces give the same default method?
The compiler reports an ambiguity and the implementing class must override the method, usually delegating explicitly to one of them. This is the diamond problem, contained by forcing an explicit choice.

You'll forget most of this by next week

That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule โ€” right before you'd lose them.

Start free for 7 days