Difference between Abstract class and Interface in Java

Difference between Abstract class and Interface in Java

4 mins read1K Views Comment
Atul Harsha
Senior Manager Content
Updated on Dec 19, 2022 00:46 IST

Both abstract class and interface in java helps in achieving abstraction. They are similar in nature yet differ from each other when it comes to their usage. Let’s see what is the difference between abstract class and interface.


Abstraction, like you all know is all about hiding the implementation details from the user. In Java abstraction is achieved through both abstract class and interface. Now that you have an option to choose, you must know when to use what. Let’s understand what is the difference between abstract class and interface in Java.

Parameter Abstract Class Interface
Definition A class typically designed to be a parent class. All the other class inherits the abstract class. An interface is the blueprint of the class consisting of abstract methods. Its consist the description of the action that an object can perform.
Declaration Declared using the keyword ‘abstract‘. Declared using the keyword ‘interface’
Implementation Abstract class can provide the implementation detail of interface. Interface cannot provide the implementation detail of abstract class
Inheritance Abstract class can be inherited using the keyword ‘extend’. Interface can be inherited using the keyword ‘implements’
Multiple Inheritance Abstract class does does not support multiple inheritance Interface supports multiple inheritance
Types of Methods Abstract class can have both abstract and non-abstract methods. Interface can only have abstract methods.
Types of Variables Supports final, non-final, static, non-static variables Supports only static and final variables
Example public abstract class language{
public abstract void java(); }
public interface class language{
void java(); }

NOTE: Abstract class achieves partial abstraction (0-100%) whereas, Interface helps in achieving complete abstraction (100%).

What is Abstract Class?

Abstract class is a class declared with ‘abstract’ keyword. Its a parent class which helps in achieving partial abstraction. It is generally used when same lines of code has to be shared among the related class. These common lines of codes are declared within the abstract class, which is then inherited by other classes using the keyword ‘extend’.

  • Abstract class: A restricted class that cannot be used to create objects. In order to access a abstract class, it must be inherited from another class.
  • Abstract method: A method defined without its body. It can only be used in an abstract class. The body is defined by the subclass inheriting it.

Implementation of Abstract Class

abstract class Shape {
int x;
int y;
// Abstract method
abstract void draw();
// Concrete method
void moveTo(int newX, int newY) {
x = newX;
y = newY;
Copy code

In this example, the Shape class is an abstract class that contains an abstract method called draw() and a concrete method called moveTo(). The draw() method must be implemented by any concrete subclass of Shape, while the moveTo() method can be used as-is by any subclass.

Compare and learn: Explore Best Java Course

What is Interface?

An interface is a blue print of class consisting of static methods and variables. Its consists of the to do list of the actions that an object can perform. For example, when you switch on a fan, all you do is hit the switch and the fan starts, you don’t have to care about how it is working. Similarly in Object Oriented Programming, an Interface is like a description of all the functions which will be implemented by the inherited class.

In order to access the interface method, it must be inherited by another class with ‘implements’ keyword. The inherited function is implemented within the sub class.

Implementation of Interface

public interface Animal {
int NUM_LEGS = 4;
void makeSound();
void move();
Copy code

In this example, the Animal interface defines two abstract methods, makeSound() and move(), and a constant called NUM_LEGS. Any class that implements the Animal interface must provide an implementation for both of these methods and must also have a constant called NUM_LEGS with a value of 4.

NOTE: Java does not support “multiple inheritance” which means that a class can only inherit from one superclass.. However, this can be achieved with interfaces, because a class can implement multiple interfaces thereby allowing to have multiple inheritance in Java. 

Summary of Difference between Abstract Classes and Interfaces in Java

Here are some key differences between abstract classes and interfaces in Java:

  • Abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods.
  • Abstract classes can contain instance variables, while interfaces cannot.
  • A class can only extend one abstract class, but it can implement multiple interfaces.
  • An abstract class can provide a partial implementation of its methods, while an interface must provide a full implementation of its methods in any class that implements it.
  • An abstract class can have constructors, while an interface cannot.
  • An abstract class can be used to provide a common base class for a group of related classes, while an interface is used to define a set of methods that must be implemented by a class.
  • An abstract class is typically used when there is a common behavior or implementation that needs to be shared among a group of related classes, while an interface is used to define a set of behaviors that a class must implement.

Explore Free Java Courses

Blogs people also read in Java:

Data Type in Java | Features of Java Programming | Jump Statement in Java | OOPS in Java | Java Interview Questions | Python vs Java | Conditional Statement in Java | Data Abstraction in Java | Super Keyword in Java | Method Overloading in Java | Array in Java | Difference between Java and Javascript | Constructors in Java | Method Overriding in Java | Data Structure and Algorithm in Java | Abstract class in Java | Loops in Java

About the Author
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio