DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Differences Between Abstract Class and Interface

When interviewing for developer positions, it's important to know the basic differences between abstract classes and interfaces.

Sebastian Malaca user avatar by
Sebastian Malaca
·
Sep. 01, 15 · Tutorial
Like (5)
Save
Tweet
Share
20.03K Views

Join the DZone community and get the full member experience.

Join For Free

It is Obvious, Right?

No, it’s not. I have conducted a lot of interviews and very often one of the first questions I used to ask was the one about the differences between interface and abstract class. And still I met a lot of programmers who couldn’t give me the right answer.

In my opinion even a junior programmer should know them, maybe not necessarily with understanding what reasons lie behind, but still - structural differences, specific for a particular language (and the same for almost all OOP languages) should be more than known.

What do I find instead? Candidates who were applying for other positions (sometimes even senior ones) who didn’t know the differences or knew only a few or one.

I know that only the things that we know very well are easy, but those are OO basics and we have to know them to write well-designed code.

So, let’s talk about basics.

Inheritance

Let’s start with one of the most known differences between an interface and an abstract class. This difference is about inheritance – any class can implement many interfaces, but can extend only one class and can have only one parent.

Multiple classes extending is a language feature that is present in a few object-oriented languages. Why? Because it brings more problems than value.

When a class has many parents and there is a situation that we have exactly the same method’s declaration in more than one, we would have to explicitly “tell” which one is the one that interests us. 

Such a code is difficult to maintain, because you have to carefully go through it whenever you are introducing any change or refactoring it. On the other hand, if one class would need to extend (at least) two classes with the same method then we are either talking about breaking DRY rule and we can extract something elsewhere, or we are messing with Single Responsibility Principle.

Well, we are messing with SRP if there’s a need of extending two classes. If each is responsible for one thing then something which has to extend both of them is responsible for… yeah, I believe you know what I mean.

“C’mon man, so if a multiple class inheritance is so bad, why is it ok to implement many interfaces?” – if a question like this appeared in your mind I have to admit that is a damn good question :)

Yet, the answer is simple. Every interface is based on a functionality as opposed to a class – on implementation. So, even if we are implementing ten different interfaces and each contains the same method’s declaration there’s no collision in it. Interface is a guarantee of method’s existence, not the way how it have to be implemented which means that as long as you aren’t violating SRP you should be ok with implementing many interfaces.

Method’s Visibility

All methods in the interface are public, but there is no such rule for those declared in abstract classes. Those ones can have any visibility except private. Why not private? Because an abstract method needs to be implemented in subclass and private is not accessible from subclass. As you can see, those two ideas are mutually exclusive.

Ok, but let’s back to the main topic. As I wrote earlier, interface is a guarantee of a functionality, you can treat it as a contract between classes which is using interface and those classes which are implementing it. It’s a guarantee that a particular class will have all declared methods implemented. That’s why those methods must be public. Any other are not important at this moment, because are strictly related to implementation.

However, it’s not the case when we talk about abstract classes. We always can have group of classes which are different in just a few aspects and except this are pretty much the same and body of their public methods is also very similar. When we find ourselves in a situation like this we can always create protected method which will be place where differences will be kept. One of the popular pattern which is using this behavior is Template Method.

Declaration vs. Definition

Interface can contain only methods’ declarations and abstract class can contain also definition of the methods.

It’s like that because the interface is focused on providing specific functionality and abstract class can be focused also on similarity in implementations of subclasses, not only on their functionality.

Constants

Both in interfaces and abstract classes there’s no problem with defining constant values. It’s because those values doesn’t depend on particular object, but are the same for all of them.

Attributes

Abstract classes can contain attributes while interfaces cannot. The reason is the same as with declarations and definitions.

Summary

Other than showing the differences, I also tried to explain the reasons it comes from. That it is not only because of whims of people who invented a particular language. It comes directly from ideas and concepts that stand behind those constructions.

I hope that I didn’t forget about anything. But if yes, then please bring it to my attention in your comments and I will try to remove mistakes as fast as I can.

And, well, good luck with your interviews :)

Interface (computing)

Published at DZone with permission of Sebastian Malaca, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 7 Ways for Better Collaboration Among Your Testers and Developers
  • Testing Repository Adapters With Hexagonal Architecture
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: