Difference Wiki

Abstract Class vs. Interface: What's the Difference?

Edited by Aimie Carlson || By Harlon Moss || Updated on October 12, 2023
An abstract class can have both methods with bodies and abstract methods (no body), while an interface can only have abstract methods.

Key Differences

Abstract classes, fundamentally, serve as a blueprint for other classes while allowing the implementation of some methods. On the other hand, interfaces only declare methods without implementing them, acting as a contract for derived classes. The abstract class permits a combination of method implementations and abstract method declarations, whereas an interface only permits abstract method declarations. It’s essential to note that an abstract class can preserve the state of an object, but an interface cannot because it can't have fields.
In contrast to an abstract class, an interface cannot contain any concrete methods; all of them must be abstract, implying no method body is provided. The abstract class provides a space where some methods can be defined fully, and others can be declared without a body. The abstract class acts as a skeletal implementation for its derivatives, while an interface sets a standard structure that derivatives must adhere to.
Inheriting from an abstract class means creating a "is-a" relationship with it, while implementing an interface indicates a "can-do" relationship. Abstract classes, with their ability to offer method implementations, can control and direct the structure of derivatives more heavily than interfaces. An interface, with its purely abstract methods, simply requires the implementing class to handle the logic, giving it a slightly different utilitarian angle.
Multiple inheritance isn’t supported in many OOP languages for abstract classes, meaning a class cannot inherit from more than one abstract class. In contrast, a class can implement multiple interfaces, providing a workaround for languages that do not support multiple inheritance. Therefore, interface offers a form of multiple inheritances and abstract class offers a singular inheritance route.
Moreover, abstract classes have access modifiers that determine the visibility of the methods and variables. Interfaces cannot have access modifiers; all methods declared in an interface are implicitly public and abstract. Abstract classes can have methods with access modifiers that direct the visibility and accessibility, while interfaces are far more open and unrestricted in this aspect.
ADVERTISEMENT

Comparison Chart

Method Implementation

Can have both implemented and abstract methods.
Cannot have implemented methods, only abstract ones.

Multiple Inheritance

Cannot be used for multiple inheritance.
Can be used for multiple inheritance.

Access Modifiers

Can have methods with access modifiers.
Cannot have methods with access modifiers.

Instance Variables

Can have instance variables.
Cannot have instance variables.

Constructors

Can have constructors.
Cannot have constructors.
ADVERTISEMENT

Abstract Class and Interface Definitions

Abstract Class

An abstract class can contain both method declarations and method implementations.
An abstract class 'Vehicle' can have a method 'start' with a body and another method 'stop' without a body.

Interface

An interface is a contract that mandates implementing classes to define specific methods.
The 'Flyable' interface mandates that any class implementing it must define a method related to flying.

Abstract Class

Abstract classes can only be subclassed, not implemented.
A class 'Dog' may extend an abstract class 'Mammal' and must provide implementations for its abstract methods.

Interface

All methods declared within an interface are implicitly public and abstract.
The 'Drawable' interface's 'draw' method is inherently public, ensuring universal accessibility across all implementing classes.

Abstract Class

An abstract class can have access modifiers to control the visibility of its methods and variables.
An abstract class 'Animal' may have a protected variable 'age' that is not accessible outside its package.

Interface

Interfaces cannot contain method implementations but only abstract method declarations.
An interface 'Swimmable' will declare a 'swim' method but not provide any details about how 'swim' is implemented.

Abstract Class

Abstract classes can have concrete methods which contain a body and provide specific functionality.
An abstract class 'Computer' can have a method 'powerOn' that includes code to power on the device.

Interface

A class can implement multiple interfaces, enabling a form of multiple inheritances.
A class 'Robot' may implement multiple interfaces like 'Walkable' and 'Talkable' to utilize multiple behaviors.

Abstract Class

Abstract classes can have constructors and variables to maintain the state of an object.
An abstract class 'Shape' might have a constructor that accepts parameters to initialize its instance variables.

Interface

An interface cannot maintain state as it cannot contain instance variables.
The 'Readable' interface can declare a 'read' method but cannot store variables like 'readingSpeed.'

Interface

A surface forming a common boundary between adjacent regions, bodies, substances, or phases.

Interface

A point at which independent systems or diverse groups interact
"the interface between crime and politics where much of our reality is to be found" (Jack Kroll).

FAQs

What is an abstract class?

An abstract class is a class that cannot be instantiated and may contain both implemented and abstract methods.

Can we instantiate an abstract class?

No, abstract classes cannot be instantiated directly.

Can an interface have implemented methods?

No, interfaces can only have abstract method declarations.

What is an interface?

An interface is a collection of abstract methods that may be implemented by any class.

Can a class extend multiple abstract classes?

No, multiple inheritance of classes is not allowed in many OOP languages.

Can a class extend an abstract class and implement an interface?

Yes, a class can extend an abstract class and implement an interface simultaneously.

What kind of methods can abstract classes have?

Abstract classes can have both abstract and concrete methods.

How many interfaces can a class implement?

A class can implement any number of interfaces.

Can abstract methods in an abstract class have access modifiers?

Yes, abstract methods in an abstract class can have access modifiers.

Can an abstract class have constructors?

Yes, abstract classes can have constructors.

Can interfaces have access modifiers for methods?

No, interface methods are implicitly public and cannot have access modifiers.

Can a class implement multiple interfaces?

Yes, a class can implement multiple interfaces.

Are methods in an interface implicitly public?

Yes, all methods in an interface are implicitly public and abstract.

Can an abstract class implement an interface?

Yes, an abstract class can implement an interface.

Why use an interface?

Interfaces define a contract for classes without imposing how methods should be implemented.

Why use an abstract class?

Abstract classes allow sharing code among several closely related classes.

Can an abstract class extend another abstract class?

Yes, an abstract class can extend another abstract class.

Can an interface extend another interface?

Yes, interfaces can extend other interfaces.

Can an interface have variables?

Yes, but they are implicitly public, static, and final (constants).

What does it mean to implement an interface?

Implementing an interface means providing implementations for all its declared methods in a class.
About Author
Written by
Harlon Moss
Harlon is a seasoned quality moderator and accomplished content writer for Difference Wiki. An alumnus of the prestigious University of California, he earned his degree in Computer Science. Leveraging his academic background, Harlon brings a meticulous and informed perspective to his work, ensuring content accuracy and excellence.
Edited by
Aimie Carlson
Aimie Carlson, holding a master's degree in English literature, is a fervent English language enthusiast. She lends her writing talents to Difference Wiki, a prominent website that specializes in comparisons, offering readers insightful analyses that both captivate and inform.

Trending Comparisons

Popular Comparisons

New Comparisons