LeetCampus
Interview Question

Can you explain the difference between an abstract class and an interface, and when you would use each?

Short Answer

An abstract class can have both abstract and concrete methods, and it can define fields, constructors, and access modifiers. It is used when you want to provide a common base for related classes that share some implementation. An interface, on the other hand, defines a contract by specifying a set of abstract methods that implementing classes must provide. It is used when you want to define a capability or behavior that disparate classes can implement, regardless of their inheritance hierarchy.

What a Strong Answer Covers

  • Abstract classes can have concrete methods and fields.
  • Interfaces only define method signatures (contracts).
  • Abstract classes are for shared behavior among related classes.
  • Interfaces are for defining capabilities for disparate classes.
  • Use abstract classes for 'is-a' relationships with shared code.

Sample Answers

Example 1: Academic Project - Understanding OOP

During my final year in college, I worked on a group project where we were tasked with creating a simple game. We needed to structure our code effectively, so I suggested using an abstract class for shared properties like 'GameCharacter', which included methods like 'move()' and 'attack()'. Each character type, such as 'Warrior' and 'Mage', inherited from this abstract class while also adding their unique implementations. This approach helped us avoid redundancy and maintain a clear hierarchy. For defining character abilities, we used interfaces, allowing different character types to implement abilities like 'CanFly' or 'CanSwim'. This clear distinction between shared behaviors and specific capabilities made our code more organized and easier to extend.

Example 2: Volunteer Experience - Organizing Events

In my time volunteering with a local non-profit, I was part of a team that organized fundraising events. I was responsible for coordinating multiple teams, each with different tasks. Here, I saw the principles of OOP in action. We could think of our main event coordinator role as an abstract class, providing common guidelines and methods for all coordinators, like 'scheduleEvent()' or 'manageBudget()'. Meanwhile, each specific event type, like 'CharityRun' or 'BakeSale', could implement their unique features, such as 'setRoute()' for the run or 'setMenu()' for the bake sale, similar to how different classes might implement an interface. This helped the team stay organized and allowed for flexibility in our planning.

Example 3: First Job Experience - Software Development Intern

As a software development intern at a tech startup, I encountered both abstract classes and interfaces while working on a project to enhance our mobile application. We created an abstract class named 'User', which contained shared properties like 'username' and 'email', along with methods like 'login()' and 'logout()'. This allowed us to establish a common foundation for different user types, such as 'AdminUser' and 'RegularUser'. Additionally, we implemented an interface called 'Notifiable', which required any user type to have a 'notify()' method, ensuring that all users could receive updates in a consistent manner. This experience solidified my understanding of when to use abstract classes for shared functionality and interfaces for establishing essential contracts.

Why Interviewers Ask This Question

** Interviewers want to assess your grasp on the differences between abstract classes and interfaces, which are fundamental to designing flexible and maintainable code. Understanding these concepts shows that you can effectively utilize inheritance and polymorphism in your programming. Common misconceptions include thinking that both serve the same purpose or that one is always better than the other.

In reality, they each have their unique applications: abstract classes are suited for shared behavior among related classes, while interfaces are ideal for defining contracts that disparate classes can implement. It's important to articulate clear use cases to illustrate your understanding. ** For instance, if you have a base class that provides some default functionality, an abstract class is suitable.

In contrast, if you want to specify a set of methods that various classes must implement, regardless of their place in the inheritance hierarchy, an interface is the way to go.

Keywords

abstract classinterfaceobject-oriented programminginheritancepolymorphism
April 13, 2026
0 views
Difficulty: Medium
Popularity: Common
Share on

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions