Photo by Max Duzij on Unsplash

The difference between Interface & Abstract Class in Java

Firas Ahmed
Javarevisited

--

Interfaces and abstract classes are two key components in Java and Object Oriented Programming that you’ll be exposed to whenever you develop a Java application or work with any Java provided classes/interfaces or even third party libraries. To get a clear idea about them, we’ll explore these two concepts in this article and learn their differences and when to use either of them.

Abstract Class

Simply put, an abstract class refers to a class that is declared using the keyword ‘ abstract’ and contains at least one abstract method (a method with no body). It can also have any number of non-abstract or concrete methods.

public abstract class Item {
// abstract method
public abstract double calculatePrice(double price);

// non-abstract method
public double calculatePrice(double price){
// code here
}
}

Interface

An interface is declared with the keyword ‘ interface’ and it contains only abstract methods, although starting from Java 8, interfaces can contain default methods.

public interface User {
// abstract method
String displayUserName(String userName);

// non-abstract method
default String displayEmail(String email) {
// code here
}
}

--

--

Firas Ahmed
Javarevisited

Hey, I'm a backend developer and I love to share what I learn with the community.