What is the difference between an interface and a class?
Object-Oriented Programming (OOP) provides powerful tools for organizing and managing code. Two fundamental concepts in OOP are interfaces and classes. Understanding the difference between them is crucial for designing flexible and maintainable software systems, especially when preparing for technical interviews.
Interface
An interface is a contract that defines a set of methods without implementing them. It specifies what methods a class should have but not how these methods should work. Interfaces are used to achieve abstraction and to allow different classes to implement the same set of methods in various ways.
Example
Imagine you have different types of payment methods in an application, such as CreditCard
, PayPal
, and BankTransfer
. You can define an interface PaymentMethod
that declares a method processPayment(amount)
. Each payment class implements this interface and provides its own implementation of the processPayment
method.
public interface PaymentMethod { void processPayment(double amount); } public class CreditCard implements PaymentMethod { @Override public void processPayment(double amount) { // Implementation for credit card payment System.out.println("Processing credit card payment of $" + amount); } } public class PayPal implements PaymentMethod { @Override public void processPayment(double amount) { // Implementation for PayPal payment System.out.println("Processing PayPal payment of $" + amount); } }
In this example, both CreditCard
and PayPal
classes implement the PaymentMethod
interface, ensuring they both have the processPayment
method, but each handles the payment processing differently.
Class
A class is a blueprint for creating objects. It defines both the data (attributes) and the behavior (methods) that the objects created from the class will have. Classes can implement interfaces and can also inherit from other classes, enabling code reuse and the creation of hierarchical relationships.
Example
Continuing with the payment example, here's how you might define a PaymentProcessor
class that uses the PaymentMethod
interface:
public class PaymentProcessor { private PaymentMethod paymentMethod; public PaymentProcessor(PaymentMethod paymentMethod) { this.paymentMethod = paymentMethod; } public void executePayment(double amount) { paymentMethod.processPayment(amount); } } // Usage public class Main { public static void main(String[] args) { PaymentMethod creditCard = new CreditCard(); PaymentProcessor processor = new PaymentProcessor(creditCard); processor.executePayment(100.0); // Outputs: Processing credit card payment of $100.0 } }
Here, the PaymentProcessor
class uses a PaymentMethod
to execute a payment. This setup allows PaymentProcessor
to work with any payment method that implements the PaymentMethod
interface, providing flexibility and extensibility.
Key Differences Between Interface and Class
Purpose
- Interface: Defines a contract with method declarations without implementations. It specifies what a class should do.
- Class: Defines both data and methods, including implementations. It specifies how something should be done.
Implementation
- Interface: Cannot hold any state (no instance variables) and cannot provide method implementations (except default methods in some languages like Java).
- Class: Can have state through instance variables and provide concrete implementations of methods.
Inheritance
- Interface: Supports multiple inheritances, allowing a class to implement multiple interfaces.
- Class: Typically supports single inheritance, meaning a class can inherit from only one superclass.
Usage
- Interface: Used to define capabilities that can be shared across different classes, promoting loose coupling.
- Class: Used to create objects with specific attributes and behaviors, encapsulating data and functionality.
Recommended Courses
To further enhance your understanding of interfaces, classes, and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the Coding Interview: Patterns for Coding Questions
- Grokking the System Design Interview
These courses offer comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.
GET YOUR FREE
Coding Questions Catalog
