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, and final by default (constants). No instance variables.
    • Example:
      interface Constants { int MAX_SPEED = 100; // Implicitly public, static, final }
  • 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 both Bird and Airplane.
  • Abstract Class:

    • Provide a base class for related objects with shared code. For example, Animal could be a base class for Dog and Cat.

Comparison Table

FeatureInterfaceAbstract Class
PurposeDefines a contract for unrelated classesProvides a base for related classes
ImplementationMust implement all methodsCan provide partial implementation
InheritanceSupports multiple inheritanceSupports single inheritance
FieldsOnly constants (public, static, final)Can have instance variables, constants
MethodsOnly abstract methods (default/static methods allowed in modern languages)Can have abstract and concrete methods
FlexibilityMore flexibleLess flexible
When to UseWhen unrelated classes need to share behaviorWhen related classes need shared code

When to Use Each

Use an Interface When:

  1. You want to define behavior for multiple unrelated classes (e.g., Flyable, Swimmable).
  2. Multiple inheritance of behavior is needed.
  3. You need a fully abstract type without any implementation.

Use an Abstract Class When:

  1. You have shared code for related classes (e.g., Animal base class for Dog and Cat).
  2. You want to provide default implementations for some methods.
  3. 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

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is concurrency in a distributed system?
What is the salary of Coinbase backend developer?
What is the first product of OpenAI?
Related Courses
Image
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.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.