Site icon DataFlair

Difference Between Abstract Class and Interface in Java

Free Java courses with 37 real-time projects - Learn Java

Hey there! So someone asked you the Difference between Abstract Class and Interface in Java and you ended up searching for it on Google? And maybe other articles did not really make you understand the topic. This question is a very popular question in interviews. There are high chances that you will be asked this one if you are applying for a Java developer role in a company.

So let us start with the differences between the two concepts!

Abstract Classes

Abstract methods, in Java, are those methods which only contain the prototype of the function and lack the body. This means that only the function name, access specifier, return type, and the parameters are known to the method itself. The method does not have a body.

If there are abstract methods in a class in Java, then the class also must be an abstract class.

A very simple example of an abstract class would be

abstract class AbstractClassName//this is the abstract class. 
{
    abstract public void abstractMethodName();//This is the abstract method. 
}

As you can see the abstractMethodName does not have a body. This type of method is an abstract method. However, it is not necessary for a class that is abstract to only contain abstract methods. An abstract class can house concrete methods as well!

Let us check out a brief program on abstract classes to refresh our memory of what they are like.

Program to illustrate the use of Abstract Classes in Java

package com.dataflair.abstractvsinterface;
abstract class AbstractClass
{
    abstract void methodabs();
}
public class Main extends AbstractClass
{
    public void methodabs()
    {
        System.out.println("We defined this inside the Main class. ");
        
    }
    public static void main(String[] args) {
        Main ob = new Main();
        ob.methodabs();
        
    }
}

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output:

We defined this inside the Main class.

Interfaces in Java

Interfaces are somewhat similar to abstract classes. They contain method prototypes that a class implements. If this is entirely new to you, check out our article on interfaces. Interfaces allow us to implement multiple interfaces in Java.

Program to illustrate the use of interfaces in Java:

package com.dataflair.abstractvsinterface;
interface CustomInterface
{
    public void methodabs();
}
public class Main implements CustomInterface
{
    //Now we will implement the method of the interface here. 
    public void methodabs()
    {
        System.out.println("We defined the definition of the interface method here.");
        
    }
    public static void main(String[] args) {
        Main ob = new Main();
        ob.methodabs();
        
    }
}

Output:

We defined the definition of the interface method here.

Note how the class implements the methods in the interface. Now that we are familiar, let us get to know the differences between these concepts.

Key Differences Between Abstract Class and Interface in Java

Parameters Interfaces in Java Abstract Classes in Java
Speed Interfaces are slow.  Abstract Classes are fast. 
Multiple Inheritance Interface can implement several interfaces. Abstract classes can extend only one class. 
Structure Can contain only abstract, default, and static methods.  There can be abstract as well as concrete methods inside an abstract class.
Used for Interfaces are useful for future enhancement. Abstract classes generally are useful to avoid independence.  
Default Implementation Before the advent of Java 8 it was very difficult to add a new abstract method in an interface. But now an interface can have default methods too.  Abstract classes can have default methods. 
Access Modifier Any method or variable inside an interface is public by default.  An abstract class can have private and protected methods inside it. 
When to use If different implementations share the same method signature, interfaces are useful.  Abstract classes are useful when implementations have common behavior. 
Access Modifier usage You cannot use access modifiers in an interface for its methods.  An abstract class allows you to set access modifiers to its methods and data. 
Usefulness When you need to define the peripheral abilities of a class, interfaces can be very useful.  Abstract classes are generally used for defining the identity of the class. 
Defined Fields You cannot define fields in an interface In abstract classes, you can define fields as well as constants. 
Constructors and Destructors As interfaces are not classes, they cannot have constructors or destructors.  On the other hand, abstract classes can have constructors and destructors.
Abstract Keyword Interfaces do not need an abstract keyword for declaring its methods as abstract. All methods inside an abstract class must have the abstract keyword. 
Class Type Interfaces can house public methods only.  Abstract classes can house private and protected abstract methods as well. 

Programs to Illustrate Differences Between Abstract Classes and Interfaces in Java

You can extend only one abstract class or a concrete class from an abstract class at a time.

package com.dataflair.abstractvsinterface;
class ConcreteClassOne{
   public void method1(){
      System.out.println("This is method 1");
   }
}
abstract class AbstractClassOne{
   public void display2(){
      System.out.println("This is method 2");
   }
}
abstract class AbstractClassTwo extends ConcreteClassOne{
   abstract void display3();
}
class ConcreteClassFour extends AbstractClassTwo{
   public void display3(){
      System.out.println("This is method 3");
   }
}
public class Demo
{
    public static void main(String[] args) {
        ConcreteClassFour object = new ConcreteClassFour();
        object.display3();
    }
}

Output:

This is method 3

A single interface can extend more than one interface at a time.

package com.dataflair.abstractvsinterface;
interface InterfaceOne
{
    public void abstractMethodOne();
}
interface InterfaceTwo
{
    public void abstractMethodTwo();

}
interface InterfaceThree extends InterfaceOne,InterfaceTwo
{
    //This interface inherits more than one interface
}

public class InterfaceDifference1 implements InterfaceThree {
    public void abstractMethodOne()
    {
        System.out.println("This is the first abstract method from the first interface");

    }
    public void abstractMethodTwo()
    {
        System.out.println("This is the second abstract method from the second interface");
    }
    public static void main(String[] args) {
        InterfaceDifference1 object = new InterfaceDifference1();
        object.abstractMethodOne();
        object.abstractMethodTwo();
    }
    
}

Output:

This is the first abstract method from the first interface
This is the second abstract method from the second interface

A single abstract class or a concrete class can inherit an abstract class.

package com.dataflair.abstractvsinterface;
class ConcreteClassOne
{
    public void concreteMethod()
    {
        System.out.println("This is a concrete method");
    }
}
abstract class AbstractClass extends ConcreteClassOne
{
    abstract public void abstractMethodOne();
}

public class AbstractClassDifference2 extends AbstractClass {
    public void abstractMethodOne()
    {
        System.out.println("This is the Abstract method");
    }
    public static void main(String[] args) {
        AbstractClassDifference2 object = new AbstractClassDifference2();
        object.abstractMethodOne();
        object.concreteMethod();
    }
    
}

Output:

This is the Abstract method
This is a concrete method

Interfaces can only extend other interfaces. A class has to implement interfaces.

package com.dataflair.abstractvsinterface;
interface InterfaceOne
{
    public void abstractMethod1();
}
interface InterfaceTwo extends InterfaceOne
{
    //Empty Interface
}

public class InterfaceDifference2 implements InterfaceTwo{
    public void abstractMethod1()
    {
        System.out.println("This is the abstract method");
    }
    public static void main(String[] args) {
        InterfaceDifference2 object = new InterfaceDifference2();
        object.abstractMethod1();
    }
    
}

Output:

This is the abstract method

Abstract classes can contain abstract and concrete methods.

package com.dataflair.abstractvsinterface;
abstract class AbstractClassOne
{
    public void concreteMethod1()
    {
        System.out.println("This is a concrete method");

    }
    abstract void abstractMethod1();
}


public class AbstractClassDifference3 extends AbstractClassOne {
    public void abstractMethod1()
    {
        System.out.println("This is an abstract method");
    }
    public static void main(String[] args) {
        AbstractClassDifference3 object = new AbstractClassDifference3();
        object.concreteMethod1();
        object.abstractMethod1();
    }
}

Output:

This is a concrete method
This is an abstract method

Interfaces can only have static, default, and abstract methods.

package com.dataflair.abstractvsinterface;
interface InterfaceNumber1
{
    public void abstractMethod1();
}

public class InterfaceDifference3 implements InterfaceNumber1{
    
    public void abstractMethod1()
    {
        System.out.println("This is the abstract method. An interface can also have default and static methods");

    }
    public static void main(String[] args) {
        InterfaceDifference3 object = new InterfaceDifference3();
        object.abstractMethod1();
    }
}

Output:

This is the abstract method. An interface can also have default and static methods

An abstract keyword is mandatory for declaring an abstract method in abstract classes.

package com.dataflair.abstractvsinterface;
abstract class AbstractClassOne
{
    abstract public void abstractMethod1();
}


public class AbstractClassDifference4 extends AbstractClassOne{
    public void abstractMethod1()
    {
        System.out.println("This is the abstract method. It is compulsory to use the abstract keyword");
    }
    public static void main(String[] args) 
    {
        AbstractClassDifference4 object = new AbstractClassDifference4();
        object.abstractMethod1();
    }
    
}

Output:

This is the abstract method. It is compulsory to use the abstract keyword

An interface does not require abstract keyword to declare its methods as abstract.

package com.dataflair.abstractvsinterface;
interface InterfaceOne
{
    abstract public void methodOne();
    public void methodTwo();
}


public class InterfaceDifference4 implements InterfaceOne{
    public void methodOne()
    {
        System.out.println("This method is abstract with the help of abstract keyword");
    }
    public void methodTwo()
    {
        System.out.println("This is an abstract method even without the help of abstract keyword");
    }
    public static void main(String[] args) 
    {
     
    InterfaceDifference4 object = new InterfaceDifference4();
    object.methodOne();
    object.methodTwo();   
    }

}

Output:

This method is abstract with the help of abstract keyword
This is an abstract method even without the help of abstract keyword

You can have protected and public abstract methods inside an abstract class.

package com.dataflair.abstractvsinterface;
abstract class AbstractClassOne
{
    abstract public void methodPublic();
    abstract protected void methodProtected();

}
public class AbstractClassDifference5 extends AbstractClassOne
{
    public void methodPublic()
    {
        System.out.println("This is a public method");
    }
    public void methodProtected()
    {
        System.out.println("This is a protected method");
    }
    public static void main(String[] args) 
    {
        AbstractClassDifference5 object = new AbstractClassDifference5();
        object.methodPublic();
        object.methodProtected();
    }
    
}

Output:

This is a public method
This is a protected method

There can only be public abstract methods inside interfaces.

package com.dataflair.abstractvsinterface;
interface InterfaceOne
{
    public void methodOne();
}

public class InterfaceDifference5 implements InterfaceOne
{
    public void methodOne()
    {
        System.out.println("Interfaces can only have public abstract methods");

    }
    public static void main(String[] args) {
        InterfaceDifference5 object = new InterfaceDifference5();
        object.methodOne();
    }
    
}

Output:

Interfaces can only have public abstract methods

An abstract class can have static, final or static final variables.

package com.dataflair.abstractvsinterface;
abstract class AbstractClassOne
{
    protected final int protecVar=50;
    public void printValue()
    {
        System.out.println("The protected Value is "+protecVar);
    }

}

public class AbstractClassDifference6 extends AbstractClassOne{
    public static void main(String[] args) {
     AbstractClassOne object = new AbstractClassDifference6();
     object.printValue();   
    }
    
}

Output:

The protected value is 50

Interfaces can only contain public static final variables.

package com.dataflair.abstractvsinterface;
interface InterfaceOne
{
    //They can have public static variables
    int publicStaticVar=50;
}

public class InterfaceExample6 implements InterfaceOne {
    public void readvalue()
    {
        System.out.println("The value of the variable is "+publicStaticVar);
    }
    public static void main(String[] args) {
        InterfaceExample6 object = new InterfaceExample6();
        object.readvalue();
    }
    
}

Output:

The value of the variable is 50

Reasons to use Abstract Classes in Java

Reasons to use Interfaces in Java

When should you use Java Interfaces?

You should use interfaces when:

When should you use Java Abstract Classes?

You should use abstract classes when:

Summary

In this article, we primarily learned about the prime differences between abstract classes and interfaces in Java. We also learned about their uses, and when to use which in programming. Strong knowledge of these concepts would aid in the better development of software.

Exit mobile version