Udemy logo

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:

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.

Page Last Updated: February 2014

Top courses in Java

Java Tutorial for Beginners
Navin Reddy
4.5 (559)
Complete Java SE 8 Developer Bootcamp - OCA Prep Included
Intertech Training, Jeff Jensen
4.6 (9,179)
Highest Rated
Complete Core Java In Simple Way
DURGASOFT NAGOOR BABU
4.7 (747)
Java Interview Help
Bharath Thippireddy
4.7 (1,193)
Design Patterns in Java
Dmitri Nesteruk
4.4 (7,955)
Java Programming for Complete Beginners
in28Minutes Official
4.5 (39,081)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (3,665)
Bestseller
Learn Selenium with Java, Cucumber + Live Project
Pavan Kumar
4.6 (4,332)
Bestseller
Modern Java - Learn Java 8 features by coding it
Pragmatic Code School
4.5 (9,604)
Core Java Made Easy (Covers the latest Java 17)
Bharath Thippireddy
4.5 (14,294)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo