Share

What Is The Difference Between Abstract Class And Interface

 
By Nidhi Nangia. Updated: November 29, 2017
What Is The Difference Between Abstract Class And Interface

Both the classes for abstract and interface are a little confusing for amateurs in Object-Oriented programming. Therefore, we are trying to make both concepts clearer and make a comparison of their usages. Basically, an abstract class just looks like an interface, but when we go deeper into the technicalities, there is a lot of difference between the two. However, even when we learn their characteristics, they can both still be a little confusing if you are not a java pro. In this oneHOWTO article, we are going to discuss about what is the difference between abstract class and interface to see if we can make understanding these processes a little better.

What are abstract class and interface?

When we talk about an abstract class, we need to understand first that it is something we cannot instantiate. Rather, we can only create a sub-class for it from some other inheritance. In simple words, we cannot instantiate it, but only inherit other classes from it. One good thing about this feature is that we can enforce the best of hierarchies from different subclasses as per our requirements and preferences. Simply put, it forces all other subclasses to follow the same standards and hierarchies that the user selected.

On the other hand, interface cannot be exactly defined as a class but an entity. It cannot be implemented, but only explained as a definition of methods without any body. One similarity between abstract class and interface is that interface can also be defined as a hierarchy for subclasses or a specific set of arguments and their methods. The major point of difference between the two is that more than a single interface can be implemented with a class, but an interface can be inherited from only one abstract class. For programs which do not support multiple inheritance, interface is used for implementing such a possibility.

The idea behind abstract class and interface

When an interface is created, basically a set of processes is created that does not have any possibility of implementation. It needs to be overridden with an implemented class. One benefit is that it gives way to a class to be a section of two classes, the interface and the inheritance hierarchy. On the other hand, when an abstract class is created, a base class is created that can have more than one finished method, but at least some methods which are abstract or incomplete. If the abstract class has all the methods incomplete, then it would be similar to an interface. Abstract class provides a definition to the base class for the way derived classes have to work. With this, programmers can fill implementation in derived classes.

Differences between abstract class and interface

There are several points on the basis of which we can differentiate between an interface and an abstract class. Here are a few of them:

Inheritance: Several interfaces may be inherited in one class, while only one abstract class can be inherited in a class

Implementation: Interface provides just a signature, but it does not provide any code. An abstract class may give complete, default code or details that need to be overridden

Java class features: Abstract class contains all the features and tools of a usual java class, except for the option of instantiating it. An abstract keyword can be used to make class abstract, while an interface is a completely different concept. It can contain public static method declarations and final constants only

Access Modifiers: Interface does not have access modifiers for functions, subs and properties, while abstract classes have them, because all members of the interface are inherently public. Here, we are talking about access specifiers of an interface member, and not the interface itself. It is allowed to provide a public access specifier, but interface members cannot have any access specifiers. The best method to declare interface is to avoid using access specifiers on the interface and the interface members as well

Peripherals: Interface defines peripheral abilities of class, but abstract class is used to define a class’ core identity where it is used for similar type objects

Keyword usage: Subclasses use ‘extends’ keywords for extending abstract class and they must provide implementation of every declared method in it unless the subclass is an abstract class also. On the other hand, subclasses use ‘implements’ keyword for implementing interfaces and must provide implementation for every method declared in it

Homogeneity: Interface is better used in implementations which share method signatures only. While abstract class is recommended if several implementations of same kind use common status or behavior

Speed: Abstract class is fast, but interface needs a longer time to identify actual methods of corresponding classes

Main Method: You can run abstract class if there is main method in it, but you cannot run an interface with it. Interface cannot be run with main method implementation

Versioning: If a new method is added to an interface, then all the implementations need to be tracked down. After this, a new definition is given to the latest method. On the contrary, if a new method is added to an abstract class, then we can provide the default implementation for it. In that case, all the existing codes will be able to work as before

Constants and fields: Interface cannot have any defined fields, while an abstract class can have them

Class and subclass: Several interfaces can be implemented by a class, but only a single abstract class can be used by a subclass

Methods: An interface needs to have all the abstract methods, but an abstract class may have non-abstract, concrete methods of implementation as well

Variables: Any variables can be declared or used by an abstract class, but an interface cannot do the same

Constructor declaration: An interface can have a constructor declaration while an abstract class cannot have one. So, before you compile your code, you need to delete the constructor declaration from an interface

Now that we know the differences between abstract class and interface, it's time to find out when to use which one.

When to use an abstract class an an inteface

Whether to use an abstract class or an interface is a decision dependent on design, as well as several other factors. If you are confused about whether to use an abstract class or interface, then here are a few recommendations.

When to use an abstract class: If you are going to create several versions of a component, then go for an abstract class. It will provide an easy and simple way to create its versions. When you will update the base class, all the inheriting classes will get automatically updated. On the other hand, you cannot change an interface after creating it. If you will want an updated version, you will need to create a new interface altogether.

When to use an interface: On the contrary, if you are going to use your functionality across several disparate objects, then it would be better to use interface. An abstract class is basically used for closely related objects, while an interface is most suitable for giving common functionalities to non-related classes. Use an interface if you are creating concise, smaller bits of functionalities, but use abstract class if you are creating bigger functional units. If you aim at providing implemented functionalities to all your component implementations, then use abstract class, as it will allow you to implement the class partially. On the other hand, an interface does not contain any implementations for any of the members.

If you want to read similar articles to What Is The Difference Between Abstract Class And Interface, we recommend you visit our Software category.

Write a comment about What Is The Difference Between Abstract Class And Interface

What did you think of this article?

What Is The Difference Between Abstract Class And Interface
What Is The Difference Between Abstract Class And Interface

Back to top