Blog header background

Abstract Class vs Interface: Understanding the Difference between Abstract Classes and Interfaces

Updated on April 23, 2024

7 min read

Copy link
Share on WhatsApp

In Java, both an abstract class and interface can describe a contract for the implementing class to satisfy. But they are not the same thing. So, what is the difference between abstract class and interface? 

Basically, the difference between abstract classes and interfaces lies in their purpose and functionality. Abstract classes can have both concrete and abstract methods, can provide default implementations, and support inheritance. Interfaces, on the other hand, can only have abstract methods, do not provide implementations, and support multiple inheritance through implementation. This article will help you understand the key differences between abstract class vs interface, two of the main building blocks of the Java programming language. 

Table of Contents:

  1. What is an Abstract Class?
  2. What is an Interface?
  3. Difference between Abstract Class and Interface
  4. Similarities between Abstract Class and Interface
  5. Abstract Class vs Interface: Use Cases
  6. Advantages and Disadvantages of Abstract Class
  7. Advantages and Disadvantages of Interface
  8. Abstract Class Vs Interface Example
  9. FAQs

brochure-banner-bg

POSTGRADUATE PROGRAM IN

Multi Cloud Architecture & DevOps

Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.

What is an Abstract Class?

An abstract class comes with an abstract keyword on the declaration. An abstract class must come with at least one abstract method. At times, an abstract class contains multiple concrete methods.

What is an Interface?

An interface is useful for implementing a class. Only abstract methods are used in the interface. The interface lacks a concrete method. 

Before we move on to understand the difference between abstract class vs Java interface, let’s look at the features of them individually.

Features of Abstract Class

Some common features of an abstract class are as follows:

  • It cannot be instantiated directly 
  • Serves as a template or blueprint for serving other classes
  • Comes with at least one pure virtual function
  • Includes abstract and non-abstract methods
  • Includes constructors and deconstructors
  • Comes with member variables
  • Serves as a base class 
  • Useful for defining a common behavior or interface shared by several related classes
skill-test-section-bg

82.9%

of professionals don't believe their degree can help them get ahead at work.

Features of Interface

Some common features of the interface in Java are as follows:

  • Defines different methods and properties that need to be implemented by a class or structure
  • Offers a common protocol to enable different software components to interact with one another
  • Helps achieve polymorphism, which enables objects of different classes to be treated as the same type
  • Enables separation of concern so that different components of a software solution can be developed independently
  • Improves code reusability by enabling different software parts to use one code base
  • Supports independent testing of software components

Explore more differences between abstract class and interface in java.

Understanding the Difference between Abstract Classes and Interfaces

The difference between abstract class and interface are as follows:

Criteria Abstract Class Interface
Final Variables An abstract class comes with non-final variables.  A Java interface comes with variables declared final by default. 
Implementation An abstract class in Java includes final, non-final, static, and non-static variables.  An interface in Java comes with static and final variables.
Default Implementation Abstract class helps with interface implementation.  Interface cannot help with the implementation of an abstract class. 
Inheritance vs Abstraction The keyword “implements” is useful for implementing a Java interface.  The keyword “extends” is useful for implementing an abstract class. 
Accessibility of Data Members A Java abstract class comes with private, protected, and other class members.  Variables or members of a Java interface are by default final.
Speed Faster than a Java Interface Slower than a Java abstract class
Structure Abstract method only Abstract and concrete methods
Data Fields Includes data fields Don’t include data fields
Inheritance An abstract class in Java supports multiple inheritance.  An interface in Java does not support multiple inheritance. 
Abstract Keyword Abstract keywords are used for declaring an abstract class.  Inheritance keywords are used for declaring a Java inheritance. 
Defining Fields Users can define fields and constants Fields cannot be defined. 

Similarities Between Abstract Classes and Interfaces

Now that you know what is the difference between abstract class and interface, learn about some of the similarities between them. The similarities between abstract class and interface are as follows:

  • Both are basic object types in Java.
  • Both include variables and methods.
  • Both can be inherited with the help of inheritances. 
  • Both are useful for achieving data abstraction in Java.

Abstract Classes Vs Interfaces: Use Cases

Abstract Classes vs Interfaces also differs in terms of the use cases.  Below are some of the examples.

The use cases of an abstract class in Java are as follows:

  • When the classes extending an abstract have multiple fields and methods in common
  • When you have non-final and non-static methods to change the object state
  • When you have particular requirements but access to only particle execution details

The use cases of an interface in Java are as follows:

  • When your problem needs to be solved with the help of multiple inheritances and includes various class hierarchies
  • When you are required to mention the behavior of a data type but not worried about who implements it

Advantages and Disadvantages of Abstract Class

Some advantages of using abstract classes in Java are as follows:

  • Can help with writing shorter codes
  • Ensures code reusability
  • Faster than interfaces

Some of the disadvantages of using Java abstract classes are as follows:

  • Cannot be instantiated
  • Don’t support multiple inheritances like interfaces

Advantages and Disadvantages of Interface

The significant advantages of using Java interfaces are as follows:

Some drawbacks of using Java interfaces are as follows:

  • Does not include the implementation of any method
  • Can’t define instance variables

Abstract Classes Vs. Interfaces: Example

Abstract Classes vs Interfaces also differs in terms of the use examples.  Below are some of the examples for both of them.

Abstract class example:

abstract class sunstar {
   
    abstract void printInfo();
}
 
class employee extends sunstar {
    void printInfo()
    {
        String name = "rahul";
        int age = 27;
        float salary = 352.2F;
 
        System.out.println(name);
        System.out.println(age);
        System.out.println(salary);
    }
}
 
class base {
    public static void main(String args[])
    {
        sunstar s = new employee();
        s.printInfo();
    }
}
Output:
rahul
23
352.2

Interface example:

// Java Program to Illustrate Concept of Interface
 
// Importing I/O classes
import java.io.*;
 
// Interface
interface Shape {
 
    // Abstract method
    void draw();
    double area();
}
 
// Class 1
// Helper class
class Rectangle implements Shape {
 
    int length, width;
 
    // constructor
    Rectangle(int length, int width)
    {
        this.length = length;
        this.width = width;
    }
 
    @Override public void draw()
    {
        System.out.println("Rectangle has been drawn ");
    }
 
    @Override public double area()
    {
        return (double)(length * width);
    }
}
 
// Class 2
// Helper class
class Circle implements Shape {
 
    double pi = 3.14;
    int radius;
 
    // constructor
    Circle(int radius) { this.radius = radius; }
 
    @Override public void draw()
    {
        System.out.println("Circle has been drawn ");
    }
 
    @Override public double area()
    {
 
        return (double)((pi * radius * radius));
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating the Object of Rectangle class
        // and using shape interface reference.
        Shape rect = new Rectangle(2, 3);
 
        System.out.println("Area of rectangle: "
                           + rect.area());
 
        // Creating the Objects of circle class
        Shape circle = new Circle(2);
 
        System.out.println("Area of circle: "
                           + circle.area());
    }
}
Output: 
Area of rectangle: 6.0
Area of circle: 12.56

Abstract Class vs Interface: Which One Should You Use?

Even though both abstract classes and interfaces are useful for data abstraction in Java, they are significantly different from one another. Knowing the difference between abstract class and interface can help with figuring out when to use what. While interfaces can help with complete abstraction, abstract classes can help with partial as well as full abstraction. 

Conclusion

Abstract Class and Interface are a core part of the Java programming language and the difference between Abstract Class and Interface is one of the popular interview questions. In this article we have learned all about Abstract Class vs Interface. The major difference between abstract classes and interfaces in Java is that abstract classes can have both concrete and abstract methods, while interfaces can only have abstract methods. Additionally, a class can extend only one abstract class, but it can implement multiple interfaces.

FAQs
Can abstract classes and interfaces be instantiated?
Abstract classes and interfaces in Java cannot be instantiated. But abstract classes can be subclassed, and interfaces can be implemented.
What are the major differences between abstract class and interface?
The major difference between abstract class and interface is that the latter cannot contain a state. But the abstract class can include states with variable instances.
When should I use an interface instead of an abstract class?
Between an abstract class and interface, you should use an interface when the functionality you are creating is useful across various disparate objects.
Can a class inherit from both an abstract class and an interface?
A regular class in Java can inherit from only one abstract class but multiple interfaces.

Updated on April 23, 2024

Link
Loading related articles...