LeetCampus
Interview Question

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

April 13, 2026
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

This question is asked to gauge a candidate's understanding of object-oriented programming (OOP) concepts. 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. In real-world applications, knowing when to use an abstract class versus an interface can significantly impact the architecture of your software. 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.

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.

Keywords

abstract classinterfaceobject-oriented programminginheritancepolymorphism

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions