DEV Community

Cover image for Difference between Interface and Abstract Class
Ben Pereira
Ben Pereira

Posted on

1

Difference between Interface and Abstract Class

On Java language realm, a question that shows up a lot on interviews, what’s the difference between interfaces and abstract classes? why both are needed?

Plain and simple would be:

  • Every class can extend only one super class (abstract class), but implement multiple interfaces, hence multiple inheritances.

  • Abstract classes can have constructors while on interface you can’t.

  • On abstract classes methods can have different access modifiers (public, private and protected) while interface only public ones.

  • Abstract classes can extend other abstract classes and implement multiple interfaces, while interfaces can only extends more interfaces.

  • Before you would say abstract classes can have concrete methods and interfaces can’t, but that changed after Java 8, now interfaces can have it as well using default keyword (keeping in mind that they will still be public, static and final).

  • On interfaces all methods are with public access modifier, static methods and final ones (can’t be overridden), while on abstract classes you can have all the access modifiers, can be static and non-static and final and non-final.

In conclusion I would say they are similar but different, used on different aspects.

  • Interfaces usually are made for firming a contract, used on factory creation classes that allows you to have a more declarative way of instances, also when you want to mock tests, or unify, generify a process, want to use something but don’t necessarily needs to know who is behind, which makes more loosely coupled, indeed needed on let’s say for example interface segregation.

  • Abstract classes idea is as a base class for concrete sub classes, something as default that can be overridden and at same time abstract methods that can be used on itself or on other classes (that’s why you have flexibility on the access modifiers). Again as a note, on Java 8 default keyword was introduced on interface, so you can also have default methods on interface that allows you to implementations up to an extent.

To finalize I will add a quote from Daniel Bell :)

Technology like art is a soaring exercise of the human imagination.

If I’m missing something or there is anything thing else to discuss feel free to drop a comment.

Image of Datadog

Get the real story behind DevSecOps

Explore data from thousands of apps to uncover how container image size, deployment frequency, and runtime context affect real-world security. Discover seven key insights that can help you build and ship more secure software.

Read the Report

Top comments (0)

Postmark Image

The email service that speaks your language

Whether you code in Ruby, PHP, Python, C#, or Rails, Postmark's robust API libraries make integration a breeze. Plus, bootstrapping your startup? Get 20% off your first three months!

Start free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay