Abstract class or Interface : Java

Oyeyinka Onaolapo
Nov 1 · 3 min read

One of the things that confuse most people when starting out with Java is distinguishing between an abstract class and an interface. Most will ask why an interface is used in place of an abstract class and vice-versa?

After implementing severally using both, I intend to explain in very simple terms the difference between an abstract class and an interface and when to use either.

This is also a common interview question as regards object-oriented programming.

Here we go:

1. First off, an abstract class can contain non-final values however variables declared in an interface are final by default.

A final variable is declared by the “final” keyword, in essence its value cannot be changed or modified. So obviously you only want to use final variables for those values that will remain constant throughout the execution of the program, this in some ways can influence your decision depending on your requirements.

2. An abstract class can have access modifiers (private, protected class members etc.) but an interface can only have public members, well technically an interface can also have default, static or abstract (starting from Java 8), however this implies the members are still public abstract.

3. An abstract class can be extended using the keyword “extends” while an interface can be implemented using the keyword “implements” as shown below:

abstract class Bank {
abstract int getLoanInterestRate();
}

class GTBank extends Bank
{


interface Bank {
int getLoanInterestRate();
}

class GTBank implements Bank
{

4. An abstract class can extend another java class and implement multiple interfaces however an interface can only extend another interface. This is very much self-explanatory.

5. An abstract class can provide the implementation of an interface; however, an interface cannot provide the implementation of an abstract class.

6. An abstract class can have certain implementation; however, an interface is completely abstract and cannot have any implementation as shown below.

public interface Engine{
public void start( );
public abstract void doIgniteCombustion();
}


abstract class Aeroplane{
String changeDirection( int deg){
System.out.println( “Bearing changed to “+ deg + “ degrees”);
}
public abstract void maintainDirection( );
}

7. An abstract class is faster than an interface.

For an interface, there is a search involved before calling any overridden method which obviously takes a bit more time. However, unless you are building or refactoring a time critical application, this time difference is extremely small and should not be noticeable.

Conclusion:

Based on the above, how or when to use either will be based entirely on what you want to achieve. This being said, if what you require is basic implementation without the need for multiple inheritance, then you can make use of an abstract class however if you require the latter, no implementation and basic design, an interface will do just fine.

Please clap, share & follow! Cheers.

Oyeyinka Onaolapo

Written by

Software Engineer | C# | Java | Angular | Ionic

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade