cft

3 reasons to start using Java interfaces

3 reasons to start using Java interfaces


user

Abdulcelil Cercenazi

3 years ago | 3 min read

what is an interface? 🚪

Oracle says: "an interface is a group of related methods with empty bodies"

I think of interfaces as a contract that a class promises the compiler to fulfill. If the class doesn't do what's promised the compiler will complain and give an error.

why use one?

Why bind myself to this contract and allow the compiler to yell at me about an unfulfilled promise?

Why don't I just write the methods inside the class and be done with it?

After spending some time in the industry I started to form an understanding of why we use them. I will list the most important three reasons from my point of view.

1 - Document APIs 📜

Let's say your team is writing a new service to be added to the poll of services your company offers, and other teams will write some other services that are going to use your service.

How will they know what method to call? what parameters to pass? what return type to expect?

Well, you guessed it, it's interfaces, the contract that ensures that those methods will always do as they describe in the interface.

Here is an interface that documents how to communicate with a registration service

You would write your own implementation of this interface, and other developers will take a look at the interface and understand what to do.

2 - Write specifications 🖋

Imagine we want to write a specification of what something should do, but not write how it should be done, and leave that detail to whoever wishes to make it happen.

Perhaps the most famous example of this is the JPA (Java Persistence API)

JPA is a set of interfaces that defines how database persistence should look in Java applications. There are a couple of implementations of these interfaces

  • Hibernate
  • Toplink
  • EclipseLink
  • Apache OpenJPA
  • and many more

Let's provide an example on "How to use a car" specification

Here we only specify what will the class that implements this interface do, and not the actual implementation of how it does it.

3- Polymorphism 💡

The third and most important use-case of interfaces is the fact that they allow us to leverage the powerful concept of Polymorphism.

  • Inheritance also allows Polymorphism. However, a class can only extend one superclass, while a class can implement as many interfaces as needed. That's why it's far more flexible.

This can make our code much more elegant and clean. But first, what was Polymorphism?

It allows us to say a thing like: "A student object can be treated as a Human Object" or "A Car object can be treated as a Vehicle object".

But, how can Polymorphism help us write clean code? Let's write some code that DOESN'T use Polymorphism.

Let's start by declaring the entity classes
Bicycle class 🚲

Car class 🚘

Truck class 🚚

Now let's write the service that will utilize those classes and run the methods of every object

First of all, we had to specify a method for each class of objects to get its instances from the map. Also, note that in the goForward method we had to pass as parameters the three types of objects (Bicycle, Car, Truck), and we had to call the goForward method for each class of objects.

Now, for three entities that isn't a big problem. However, when you have 10 or even a 100 ones, that quickly becomes a problem.

Let's apply interfaces and Polymorphism and see the results

We start by defining the interfaces,
The Transport interface

The Vehicle interface which extends the Transport interface

The entities classes note that we make the classes implement the interfaces.
🚲 🚘 🚚

The service that utilizes the classes Polymorphism


What did the interface and Polymorphism help us do? 💪

  • Gather those objects that are of the compatible interface (Vehicle,
    Transport) in a single List data structure.
    • The goForward method handles all objects that implement the interface Transport in a single command and triggers the specific implementation of each one.
    • The same goes for the checkEngine method, which handles all objects that implements the Vehicle interface.
    • Our code got much cleaner this way!!!

Conclusion 👇🏻

Interfaces have three main usages

  1. building and documenting APIs
  2. writing specification for other people to implement
  3. making use of Polymorphism to write readable clean code.


Source code 👨‍💻GitHub

Upvote


user
Created by

Abdulcelil Cercenazi

I love to learn and teach. Follow me for Software development/Clean coding topics :)


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles