More
Masterclasses
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.
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.
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.
Some common features of an abstract class are as follows:
Some common features of the interface in Java are as follows:
Explore more differences between abstract class and interface in java.
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. |
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:
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:
The use cases of an interface in Java are as follows:
Some advantages of using abstract classes in Java are as follows:
Some of the disadvantages of using Java abstract classes are as follows:
The significant advantages of using Java interfaces are as follows:
Some drawbacks of using Java interfaces are as follows:
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
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.
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.
Abstract classes and interfaces in Java cannot be instantiated. But abstract classes can be subclassed, and interfaces can be implemented.
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.
Between an abstract class and interface, you should use an interface when the functionality you are creating is useful across various disparate objects.
A regular class in Java can inherit from only one abstract class but multiple interfaces.
Blogs from other domain
Carefully gathered content to add value to and expand your knowledge horizons