What is the difference between an Interface and an Abstract class?
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Understanding the difference between an interface and an abstract class is crucial for designing flexible and maintainable object-oriented systems. While both are used to define a blueprint for other classes, their purposes and capabilities vary.
Key Differences Between Interface and Abstract Class
Definition
- Interface: Specifies a contract or set of rules that a class must follow. It focuses on what a class must do, but not how it does it.
- Abstract Class: Serves as a partially implemented blueprint for related classes. It defines what a class must do and may provide some implementation for how.
Purpose
- Interface: Best for defining behavior that can be applied to unrelated classes.
- Abstract Class: Best for creating a base for related classes that share common functionality.
Detailed Comparison
1. Implementation
-
Interface:
- A class implementing an interface must provide implementations for all its methods.
- Example:
interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("Bark"); } }
-
Abstract Class:
- A subclass inheriting from an abstract class can inherit fully implemented methods and override abstract ones.
- Example:
abstract class Animal { void eat() { System.out.println("This animal eats food."); } abstract void makeSound(); } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } }
2. Multiple Inheritance
-
Interface:
- A class can implement multiple interfaces, enabling multiple inheritance of behaviors.
- Example:
interface Swimmable { void swim(); } interface Flyable { void fly(); } class Duck implements Swimmable, Flyable { public void swim() { System.out.println("Duck swims"); } public void fly() { System.out.println("Duck flies"); } }
-
Abstract Class:
- A class can inherit from only one abstract class due to single inheritance.
- Example:
abstract class Bird { abstract void fly(); } class Sparrow extends Bird { void fly() { System.out.println("Sparrow flies"); } }
3. Fields and Constants
-
Interface:
- Fields in an interface are
public
,static
, andfinal
by default (constants). No instance variables. - Example:
interface Constants { int MAX_SPEED = 100; // Implicitly public, static, final }
- Fields in an interface are
-
Abstract Class:
- Can have instance variables, static variables, and constants.
- Example:
abstract class Vehicle { protected int speed; }
4. Method Implementation
-
Interface:
- Prior to Java 8: No method implementation.
- From Java 8 onwards: Default and static methods are allowed.
- Example:
interface Animal { default void sleep() { System.out.println("Sleeping..."); } void makeSound(); }
-
Abstract Class:
- Can have fully implemented methods, abstract methods, or both.
- Example:
abstract class Animal { void eat() { System.out.println("This animal eats food."); } abstract void makeSound(); }
5. Use Case
-
Interface:
- Define a set of actions or behaviors for unrelated objects. For example,
Flyable
can be implemented by bothBird
andAirplane
.
- Define a set of actions or behaviors for unrelated objects. For example,
-
Abstract Class:
- Provide a base class for related objects with shared code. For example,
Animal
could be a base class forDog
andCat
.
- Provide a base class for related objects with shared code. For example,
Comparison Table
Feature | Interface | Abstract Class |
---|---|---|
Purpose | Defines a contract for unrelated classes | Provides a base for related classes |
Implementation | Must implement all methods | Can provide partial implementation |
Inheritance | Supports multiple inheritance | Supports single inheritance |
Fields | Only constants (public, static, final) | Can have instance variables, constants |
Methods | Only abstract methods (default/static methods allowed in modern languages) | Can have abstract and concrete methods |
Flexibility | More flexible | Less flexible |
When to Use | When unrelated classes need to share behavior | When related classes need shared code |
When to Use Each
Use an Interface When:
- You want to define behavior for multiple unrelated classes (e.g.,
Flyable
,Swimmable
). - Multiple inheritance of behavior is needed.
- You need a fully abstract type without any implementation.
Use an Abstract Class When:
- You have shared code for related classes (e.g.,
Animal
base class forDog
andCat
). - You want to provide default implementations for some methods.
- You need instance variables or non-constant fields.
Summary
- Interfaces focus on "what to do" and are best for unrelated classes sharing behavior.
- Abstract Classes focus on "how to do it" for related classes and provide reusable implementations.
For learning more about these principles and their applications in system design, explore Grokking System Design Fundamentals and Grokking the System Design Interview on DesignGurus.io. These resources provide practical insights into software design patterns and object-oriented principles.
TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.