avatarYujian Tang

Summary

The provided web content distinguishes between interfaces and abstract classes in Object-Oriented Programming (OOP), particularly in Java, highlighting their differences, similarities, and use cases.

Abstract

The article "What’s the Difference Between an Interface and an Abstract Class?" delves into the nuances of Object-Oriented Programming (OOP) by comparing interfaces and abstract classes. It emphasizes that both are used to define and shape the structure of classes but serve different purposes. Interfaces are contractual templates that outline methods and variables without providing implementations, ensuring consistent interaction with the methods that implement them. In contrast, abstract classes can include both abstract methods and implemented methods, allowing for a higher level of class definition and partial implementation. The article also discusses the advantages of using interfaces for total abstraction and multiple inheritances, while abstract classes are preferred for defining the type of object and providing shared actions with potential variations in implementation. The content is particularly relevant to junior software engineers aiming to accelerate their career growth by mastering OOP concepts.

Opinions

  • Interfaces are seen as "total abstractions" and represent black boxes for classes, defining a contract for application functionality without specifying the internal workings of the methods.
  • Abstract classes are viewed as a means to create a higher-level class that provides a shared foundation for subclasses, with the flexibility to include both abstract and concrete methods.
  • The author suggests that interfaces are best used when the focus is on what a class does (actions), while abstract classes are more appropriate when defining what an object is (type).
  • The article promotes the idea that understanding these OOP concepts is crucial for career advancement in software engineering, particularly for those aiming to move from junior to senior positions.
  • It is implied that the use of multiple interfaces is beneficial for creating classes with diverse functionalities, such as different aspects of Natural Language Processing (NLP).
  • The author advocates for the use of abstract classes to encapsulate common behaviors and protect member variables that do not need to be exposed for interaction, aligning with principles of encapsulation and information hiding in OOP.

Object-Oriented Programming

What’s the Difference Between an Interface and an Abstract Class?

Interfaces vs Abstract Classes for Object-Oriented Programming

Image from Wikimedia

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

Image from Wikimedia

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

Image from maxpixel

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

Image from slidetodoc

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!

Further Reading

Object Oriented
Oop Concepts
Java
Software
Career Development
Recommended from ReadMedium