Difference between abstract class and interface

difference between abstract class and interface

Difference between abstract class and interface

Abstract classes and interfaces are fundamental tools in object-oriented programming (OOP) used to achieve abstraction. An abstract class is a class that cannot be instantiated and is used to provide a common base for related subclasses, while an interface is a contract that defines a set of methods that any class must implement, regardless of where that class sits in the inheritance tree.

[Quick Examples / Primary Types]:

  • Abstract Class (Animal): Defines common properties like species and methods like eat(), but leaves makeSound() for specific animals to define.
  • Interface (Flyable): A contract that can be applied to Bird, Airplane, or SuperHero to ensure they all implement a fly() method.
  • Abstract Class (BasePage): In web development, used to hold shared setup logic for all page objects.
  • Interface (Serializable): A marker that tells the system an object’s state can be converted into a byte stream.

[Table of Contents]

  1. Key Characteristics and Mechanics
  2. When to Use Which?
  3. Comparison Table
  4. Summary Table

[Key Characteristics and Mechanics]

In modern programming languages like Java, C#, and C++, the choice between these two depends on the relationship between objects. An abstract class creates an “is-a” relationship (e.g., a Dog “is-a” Animal). It can contain both abstract methods (no body) and concrete methods (with a body), as well as member variables (fields).

An interface, conversely, establishes a “can-do” or “acts-as” relationship. Since most languages do not support multiple inheritance for classes, interfaces are the primary way to allow a class to take on multiple behaviors.

:light_bulb: [Practical Tip:] Use an abstract class when you want to share code among several closely related classes. Use an interface when you want to define a capability that can be shared by completely unrelated classes.


[When to Use Which?]

Use an Abstract Class if:

  • You want to share code (implementation) among several closely related objects.
  • You need to define non-static or non-final fields to save the state of an object.
  • You expect classes extending the abstract class to have many common methods or fields.

Use an Interface if:

  • You want to achieve multiple inheritance (a class can implement many interfaces).
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  • You want to decouple the definition of a contract from its implementation.

[Comparison Table]

Feature Abstract Class Interface
Relationship “Is-a” (Inheritance) “Can-do” (Contract)
Multiple Inheritance No (A class can only extend one) Yes (A class can implement many)
Implementation Can have method bodies (Concrete) Historically only abstract (Default methods in Java 8+)
Variables Can have final, non-final, static, non-static Usually only public static final (Constants)
Constructors Can have constructors Cannot have constructors
Access Modifiers Can be private, protected, or public Usually strictly public

[Summary Table]

Key Point Details
[Definition] Base class for related objects vs. Contract for behaviors.
[Key Examples] Shape class (Abstract) vs. Drawable (Interface).
[Main Impact] Abstract classes reduce code duplication; Interfaces provide high flexibility and decoupling.

[Next Steps]

Would you like me to provide a code example in a specific language like Java, Python, or C# to show these differences in action?