Object-Oriented Programming
What’s the Difference Between an Interface and an Abstract Class?
Interfaces vs Abstract Classes for Object-Oriented Programming

When I first started programming I hadn’t even heard about interfaces, much less abstract classes. However, one of the first things you should do as a junior software engineer or developer if you want to fast-track your promotions is to learn about abstractions and Object-Oriented Programming (OOP) concepts.
Two of the concepts of OOP are interfaces and abstract classes. Interfaces and abstract classes are quite similar but have different uses. In this article, we’ll cover the differences and similarities between interfaces and abstract classes and then take a deeper dive into both concepts.
Please note that most of this article will be speaking about the Java concepts of interfaces and abstract classes.
Interfaces vs Abstract Classes
Interfaces and abstract classes are both abstractions used to help define classes, but they do it in different ways. Let’s take a look at the differences and similarities of interfaces and abstract classes.
Similarities Between Interfaces and Abstract Classes
Both interfaces and abstract classes are used to create classes. The two key similarities between interfaces and abstract classes are that they both cannot be implemented, and both contain sets of methods that are defined and must be declared in their implementation.
Differences Between Interfaces and Abstract Classes
The first difference between interfaces and abstract classes is what they are and how they are used to implement classes. Interfaces are implemented and abstract classes are extended.
The main implementation differences between interfaces and abstract classes mostly lie in what you’re allowed to do with abstract classes that you’re not allowed to do with interfaces.
- Interfaces cannot have implemented methods, abstract classes can
- All interface variables are static and final, abstract classes may have non-final, non-static variables
- Interfaces can only extend other interfaces, abstract classes may implement multiple interfaces and extend other classes
- Abstract classes may have private or protected members while interface members are public by default.
Illustrated Example

Let’s take the diagram above as an example. In this example, “Character” could be the interface. “Hero” could be a class that implements that interface and “Monster” could be an abstract class that implements that interface. Then “Dragon” and “Orc” could be classes that implement the “Monster” abstract class.
Interfaces

Interfaces are contractual templates used to define how you can interact with the methods that implement them. They do not declare how the functions will work internally, but merely define the existence of certain functions and variables.
Many of us are most familiar with interfaces in the case of application programming interfaces or APIs. APIs are a black box in which you send data in a pre-specified format and return data in a pre-specified format back to you. This is the exact abstraction that interfaces are used for, they stand as black boxes for classes.
Why Use an Interface
Interfaces are “total abstractions”. As we said above, interfaces represent black boxes for classes. They do not implement any methods, they only define what they are. We use them as predefined contracts for application functionality.
If you are creating a class, A, and Class A has to do action x then you would want to use an interface I which defines a function to do x. For example, perhaps you have a class that does Named Entity Recognition (NER), then you’d want to implement an interface that defines a NER function.
A class can implement multiple interfaces. If we want to create a Natural Language Processing (NLP) class, then we could implement multiple interfaces in it. Perhaps an interface for NER, Sentiment Polarity, and AI Text Summarization.
How to Use an Interface
In Java, we use interfaces through the implements keyword. When a class implements an interface, it must declare and implement each method in an interface. The only exception to this is if the class is declared abstract, then it is an abstract class and is allowed to not implement every method. One class may implement multiple interfaces.
Abstract Classes

Abstract classes are a type of class that isn’t fully implemented. In the visual example above, an “Animal” is the abstract class while “Dog” and “Cat” are concrete classes.
Why Use an Abstract Class
We use abstract classes when we want a “higher level” of a class. For example, dogs and cats are animals, or from the one further up, orcs and dragons are monsters. If you can say A is a B then you would use the abstract class B.
Abstract classes usually contain some partially implemented code or some protection around its members. Remember that all the members of an interface are public by default, that’s because interfaces are used to define how an object can be interacted with.
Abstract classes define what an object does or have that does not need to be interacted with. It’s preferable that we keep variables that need not be interacted with outside of their scope private or protected.
How to Use an Abstract Class
An abstract class is declared using the abstract keyword. The unimplemented methods in an abstract class must also be declared abstract. Regular classes use abstract classes with the extends keyword. In Java, you can only extend one abstract class per class.
Summary of Interfaces vs Abstract Classes
In this post, we learned about interfaces and abstract classes, what they are, and when to use them.
Interfaces define how an object is interacted with. They’re used when we can say “Class A does action x" where x could be the interface or a function in it. We use interfaces when we have multiple class hierarchies and we want to specify the behavior of a data type but don’t care who implements that behavior.
For example, sight and smell would be declared in the interface for an animal. All animals have sight and smell, but they are implemented differently.
Abstract classes define the type of object something is. They’re used when we can say “Class A is a B", where B would be the abstract class. We use abstract classes when we have multiple classes that perform some shared set of actions that may or may not be implemented the same way.
For example, eyes and nose would be declared in the abstract class of a mammal. All mammals have eyes and noses that may or may not work in the same ways.
If you liked this article, please share it on Twitter! For unlimited access to Medium articles, sign up to become a Medium Member today! Don’t forget to follow me, Yujian Tang, for more articles on technology, natural language processing, and growth!





