Difference between abstract class and interface in java

Let’s learn what is the difference between abstract class and interface in java?

Difference between abstract class and interface in java

Here’s the difference between abstract class and interface.

Abstract classInterface
An abstract class can have protected, private etc. as access modifiers for class members.All methods in interfaces are public and abstract by default.
In Abstract class we can declare fields that are not static and final, and define public, protected and private methods.Interface only have static and final variables.
Abstract class do not support multiple inheritance.Interface support multiple inheritance in java.
Abstract class is declared using “abstract” keyword.Interface keyword is used to declare interface.
Abstract class can extend only one parent class but can implement multiple interfaces.An Interface can extend any number of interfaces.
Abstract class can be extended using “extends” keyword.Interface can be implemented using “implements” keyword.
Abstract class can have non-abstract and abstract methods.Interface can have only abstract methods. From Java 8 Interface can have static and default methods.
Achieves partial abstraction.Achieves full abstraction.

Example on difference between abstract class and interface – where abstract class extends only one class or one abstract class

class A
{
   public void print1()
   {
      System.out.println("print1 method");
   }
}
abstract class B
{
   public void print2()
   {
      System.out.println("print2 method");
   }
}
abstract class C extends A
{
   abstract void print3();
}
class D extends C
{
   public void print3()
   {
      System.out.println("print3 method");
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      D obj = new D();
      obj.print3();
   }
}

Output:

print3 method


Example on difference between abstract class and interface – where abstract class is extended by a class

class A
{
   public void print1()
   {
      System.out.println("print1 method");
   }
}
abstract class B
{
   public void print2()
   {
      System.out.println("print2 method");
   }
}
abstract class C extends B
{
   abstract void print3();
}
class D extends C
{
   public void print2()
   {
      System.out.println("D class - print2 method");
   }
   public void print3()
   {
      System.out.println("print3 method");
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      D obj = new D();
      obj.print2();
   }
}

Output:

D class – print2 method


Example on difference between abstract class and interface – where abstract class can have regular and abstract methods

abstract class A
{
   abstract void print1();
   public void print2()
   {
      System.out.println("print2 method");
   }
}
class B extends A
{
   public void print1()
   {
      System.out.println("print1 method");
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      B obj = new B();
      obj.print1();
   }
}

Output:

print1 method


Example on difference between abstract class and interface – where to declare an abstract method keyword ‘abstract’ is mandatory

abstract class A
{
   public abstract void print1();
}
class B extends A
{
   public void print1()
   {
      System.out.println("print1 method");
   }
   public void print2()
   {
      System.out.println("print2 method");
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      B obj = new B();
      obj.print1();
   }
}

Output:

print1 method


Example on difference between abstract class and interface – where abstract class can have public and protected abstract methods

abstract class A
{
   protected abstract void print1();
   public abstract void print2();
   public abstract void print3();
}
class B extends A
{
   public void print1()
   {
      System.out.println("print1 method");
   }
   public void print2()
   {
      System.out.println("print2 method");
   }
   public void print3()
   {
      System.out.println("print3 method");
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      B obj = new B();
      obj.print1();
   }
}

Output:

print1 method


Example on difference between abstract class and interface – where abstract class can have static final, static or final variables

abstract class A
{
   private int x1 = 2;
   protected final int x2 = 4;
   public static final int x3 = 6;
   public void print1()
   {
      System.out.println("First Number = " + x1);
   }
}
class B extends A
{
   public void print2()
   {
      System.out.println("Second Number = " + x2);
      System.out.println("Third Number = " + x3);
   }
}
class AbstractClassExample
{
   public static void main(String[] args)
   {
      B obj = new B();
      obj.print1();
      obj.print2();
   }
}

Output:

First Number = 2
Second Number = 4
Third Number = 6


Also read – java overview