Abstract Class and Interface

Omer Shafique
6 min readSep 8, 2022

--

The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class. Explore more differences between abstract class and interface.

An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is an Abstract Class?

What is an Interface?

Difference between Abstract Class and Interface

When to use an abstract class vs. interface

What is an Abstract Class?

A class that has the abstract keyword in its declaration is called an abstract class. Abstract classes should have at least one abstract method. , i.e., methods without a body. It can have multiple concrete methods.

Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.

Abstract classes cannot be instantiated.

abstract class Shape {
int b = 20;
abstract public void calculateArea();
}

public class Rectangle extends Shape {
public static void main(String args[]) {
Rectangle obj = new Rectangle();
obj.b = 200;
obj.calculateArea();
}
public void calculateArea() {
System.out.println("Area is " + (b * b));
}
}

What is an Interface?

The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.

An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)

interface Pet {
public void test();
}
class Dog implements Pet {
public void test() {
System.out.println("Interface Method Implemented");
}
public static void main(String args[]) {
Pet p = new Dog();
p.test();
}
}

Difference between Abstract Class and Interface

As we know that abstraction refers to hiding the internal implementation of the feature and only showing the functionality to the users. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction

Interfaces are used to define contracts for the subclasses whereas abstract classes also define contracts but they can provide other methods implementations for subclasses to use.

  1. Type of variables:

An abstract class can have all four; static, non-static, and final, non-final variables, whereas final and static variables, are used in Interface.

2. Accessibility of Data Members:

The abstract class has class members like private and protected, etc. In the interface, class members are public by default.

3. Type of methods:

An abstract class can contain both abstract and non-abstract methods. whereas Interface contains only abstract methods.

Abstract classes can have methods with implementation whereas the interface provides absolute abstraction and can’t have any method implementations.

4. Declaration

To declare abstract class abstract keywords are used & The interface can be declared with the interface keyword.

5. Constructor

Abstract classes can have constructors but interfaces can’t have constructors.

6. Inheritance vs Abstraction

Subclasses use extends keyword to extend an abstract class and they need to provide the implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use the implements keyword to implement interfaces and should provide the implementation for all the methods declared in the interface.

7. Multiple implementations

A subclass can extend only one abstract class but it can implement multiple interfaces.

Abstract classes can extend other classes and implement interfaces but the interface can only extend other interfaces.

8. Multiple Inheritance:

The interface supports multiple inheritances; an abstraction does not support multiple inheritances.

When to use an abstract class vs. interface

Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications.

When designing applications, it is important to know when to use an abstract class and when to use an interface. Although abstract classes and interfaces seem similar in some ways, there are key differences that will determine which is the best choice for what you’re trying to accomplish. In this article, We’ll discuss those differences and how to decide when to use which.

Consider using abstract classes if any of these statements apply to your situation:

  • In the application, there are some related classes that need to share some lines of code then you can put these lines of code within the abstract class and this abstract class should be extended by all these related classes.
  • You can define the non-static or non-final field(s) in the abstract class so that via a method you can access and modify the state of the object to which they belong.
  • You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  • If there are a lot of methods in the contract, then the abstract class is more useful because we can provide a default implementation for some of the methods that are common for all the subclasses. Also if subclasses don’t need to implement a particular method, they can avoid providing the implementation but in the case of interface, the subclass will have to provide the implementation for all the methods even though it’s of no use and implementation is just an empty block.
  • If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with the abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.

Consider using interfaces if any of these statements apply to your situation:

  • It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.
  • You want to specify the behavior of a particular data type but are not concerned about who implements its behavior.
  • Languages which doesn’t support multiple class level inheritance, so every class can extend only one superclass. But a class can implement multiple interfaces. So most of the time Interfaces are a good choice for providing the base for class hierarchy and contract. Also coding in terms of interfaces is one of the best practices for coding.

Use Abstract classes and Interface both

  • Using interfaces and abstract classes together is the best approach to designing a system. For example, in Java JDK java.util.List is an interface that contains a lot of methods, so there is an abstract class java.util.AbstractList that provides a skeletal implementation for all the methods of List interface so that any subclass can extend this class and implement only required methods. We should always start with an interface as the base and define methods that every subclass should implement and then if there are some methods that only a certain subclass should implement, we can extend the base interface and create a new interface with those methods. The subclasses will have the option to choose between the base interface or the child interface to implement according to its requirements. If the number of methods grows a lot, it’s not a bad idea to provide a skeletal abstract class implementing the child interface and providing flexibility to the subclasses to choose between an interface and an abstract class.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

--

--