LeetCampus
Interview Question

Can you explain the difference between a class and an interface in object-oriented programming?

June 28, 2026
0 views
Difficulty: Medium
Popularity: Moderate
Share on

Question Explanation

Understanding the distinction between classes and interfaces is crucial in object-oriented programming (OOP). Interviewers ask this question to gauge a candidate's foundational knowledge of OOP principles. They want to see if you comprehend not just the definitions, but also the real-world applications of these concepts. A common misconception is that classes and interfaces serve the same purpose; however, they are fundamentally different. Classes are blueprints for creating objects and can contain both data and methods, while interfaces are contracts that define a set of methods without implementing them, thus enforcing a structure that implementing classes must follow. This difference is vital in designing flexible and maintainable software systems. When candidates articulate this distinction clearly, it demonstrates their understanding of encapsulation, inheritance, and polymorphism in OOP, making them more appealing to potential employers. In practical terms, you might use an interface to define a common behavior that different classes can implement, allowing for a more modular and decoupled code structure.

Sample Answers

Example 1: College Project - [Library Management System]

In my final year at college, I worked on a project called a Library Management System. We used classes to represent different entities like 'Book', 'Member', and 'Librarian'. Each class had its own attributes and methods, allowing us to create objects that could interact with each other. For instance, the 'Book' class had a method to check availability, while the 'Member' class had methods to borrow and return books. We also implemented an interface called 'Searchable' to define a method 'search()' that both 'Book' and 'Member' classes had to implement. This helped us ensure that any object that needed to be searchable followed the same structure, making our system more organized and easier to manage.

Example 2: Volunteer Experience - [Community Service Project]

During my time volunteering at a local community center, I was part of a team that organized events for different age groups. We created a class called 'Event' that included details like the event name, date, and participants. Additionally, we defined an interface called 'Registerable' that included a method 'register()'. Various event types, like 'Workshop' and 'Seminar', implemented this interface, allowing us to handle registrations uniformly. This experience taught me the importance of using interfaces to maintain consistency across different components while still allowing for unique characteristics in each event type.

Example 3: First Job Experience - [Junior Developer Role]

In my first job as a junior developer, I worked on a project that involved creating a user management system. Here, we used classes to define user roles such as 'Admin', 'Editor', and 'Viewer'. Each role class had specific permissions and methods related to their functions. We also created an interface called 'Authenticatable' that required the classes to implement methods like 'login()' and 'logout()'. This structure allowed us to add new user roles easily without changing the core logic, demonstrating the flexibility that interfaces provide in OOP. It was a practical application of the concepts I learned in college, and it helped me understand how to write scalable code.

Keywords

class vs interfaceobject-oriented programmingOOP conceptssoftware designprogramming fundamentals

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions