The Difference between Abstract Class and Interface in Java

difference between abstract class and interfaceThe concept of abstract classes and interfaces may often be confusing because they are used in similar situations and provide a similar functionality. There are several major differences between the two, both in how we define and how we use them, which we’ll be looking at in this article. If you’re not familiar with object oriented programming concepts, we recommend you first try this course on object oriented programming.

The Concept of Object Oriented Programming

Languages like Java and C# are object oriented languages. This means that they use programming “objects” that emulate real world objects. Programs in these languages perform programming tasks via these objects.

As you’ll learn in this course on programming, a class in object oriented programming is a template, or a blueprint, from which objects are derived. You could say that a Mercedes Benz has been derived from a class called Cars. This makes the Mercedes Benz your object, while your class will be Cars.

Object oriented languages like Java support the concept of inheritance. This means that an object can “inherit” the behaviors and methods of a class. Whenever you create new object of a class, you say that you are instantiating it. You need to have a basic grasp of object oriented concepts to clearly understand abstract classes and interfaces.

Abstract Classes

So what are abstract classes exactly? Take this beginner’s course on Java to find out in greater detail, but let’s go over the basics. An abstract class is a class that you can’t instantiate. It will let other classes inherit from it, but it cannot be instantiated by itself. The only purpose of the abstract class is to let other sub classes inherit from it. It can be used to impose guidelines and hierarchies on sub classes.

Here is a simple abstract class:

abstract class Cars
{
    int gas;
    int getGas()
    {
    return this.gas;
    }
    abstract void run();
}
class Merc extends Cars
{
   void run()
{
 print("Fast");
}
}

Explanation: In the first part of the code, you are declaring an abstract class “Cars”. You are creating a guideline which the other classes in your program can derive from. The behavior of your abstract class “Cars” will be inherited by subclasses.

Interface

An interface is not a class, like the abstract class, but it is very similar to it. It contains methods without a signature (a body). An interface cannot do anything on its own. Think of it as an empty template that you can copy and fill. It, too, is used to impose guidelines and hierarchies and provide methods to sub classes. A class cannot inherit from more than one abstract class at one time in languages like Java and C. Because of the lack of support for multiple-inheritance, interfaces are used instead.

Here is a simple interface and a class that inherits from it:

interface Cars
{
     void run();
     int getGas();
}
class Merc implements Cars
{
    int gas;
    void run()
    {
    print("Faster");
    }
    int getGas()
    {
    return this.gas;
    }
}

Explanation: As you can see, the interface is empty, unlike the abstract class we declared earlier, which had behaviors.

Differences between Abstract Classes and Interface

An interface contains a set of methods that haven’t been implemented. A class that references the interface must override these methods. This allows the class to be a part of two classes at one time (multiple-inheritance) – once as a normal sub-class and once as a “sub-class” of an interface.

An abstract class, like an interface, will contain methods. However, there always will be at least one method that hasn’t been completed. This is one major difference between an abstract class and an interface. The abstract class will provide a guideline (a base class definition) from which derived classes will begin. You, the programmer, will be able to implement these derived classes. You can only define abstract methods in abstract classes. However, it’s not necessary that you define an abstract method when you define an abstract class.

Apart from this major difference, here are some other differences between the two:

  • A class can only inherit from one abstract class at a time. However, a class may inherit from multiple interfaces. Interfaces are used to implement the concept of multiple inheritance in object oriented programming.

  • Because an abstract class is a real class, it can have access modifiers for its functions and properties, like for regular classes. Because an interface is not a class, it does not allow access modifiers.  Everything is considered public (open to everything) by default.

  • An interface is just an empty signature and does not contain a body (code). An abstract class can provide code, i.e., methods that have to be overridden.

  • Abstract classes are used when we require classes to share a similar behavior (or methods). However, if we need classes to share method signatures, and not the methods themselves, we should use interfaces.

  • We could say that an abstract class is processed faster by the pc, but it depends on the code we have written. Sometimes an interface is faster (because it’s just a bunch of empty names). Sometimes an abstract class is processed faster, as the pc doesn’t need to refer to derived class for a method.

  • It takes more time to add new methods to an interface. Code has to be rewritten for the interface and for all classes that refer to it include the new methods. It’s easier to add code to an abstract class, because we can use it as the default implementation. The program will still continue to run properly.

  • Because an interface is empty, it cannot have constants or fields. An abstract class can contain fields and constants definitions.

  • Interfaces can add to the existing functionality of a class. They are not necessarily integral to the identity of the classes that reference them. Abstract classes, on the other hand, provide an identity to the classes that derive from them, as they inherit their behaviors from it.

Summing up, we can say that the major difference between an abstract class and an interface is the methods they contain (completed versus uncompleted) and their contents (a real class vs. an empty template). If you’re unsure whether to use an abstract class or an interface, make sure you better understand advanced Java programming concepts to make a more informed choice.