Differences between abstract class and interface in Java

One of the most frequently asked questions on Java job interview: "What the difference between abstract class and interface in Java?" And in this post I will try to answer that question.

The differences are following:

  1. The number of extendings. An abstract class can extend only one typical Java class or one abstract class at a time. But an interface can extend any number of interfaces at a time.
  2. What can extend. An abstract class can extend concrete (regular) class or abstract class. But interface can extend another interface only.
  3. Keyword "abstract" for methods. Keyword "abstract" is mandatory for declaring a method as an abstract in abstract class. But in interface that keyword is optional, because methods abstract here by default.
  4. Access modifiers for methods. An abstract class can have protected and public abstract methods. But interface can have public abstract methods only.
  5. Variables. An abstract class can have static, final or static final variables with any access specifier. But interface can have constant variables only (that implicitly public static final).

Now, let's view this differences on examples.

Difference 1. The number of extendings

An abstract class can extend only one typical Java class or one abstract class at a time.

// Define regular class with regular method 
class ClassOne{
   public void methodOne(){
      System.out.println("This is methodOne");
   }
}

// Define abstract class with regular method 
abstract class ClassTwo{
   public void methodTwo(){
      System.out.println("This is methodTwo");
   }
}

// Define abstract class that extends regular class and introduces new abstract method 
abstract class ClassThree extends ClassOne{
   abstract void methodThree();
}

// Define regular class that extends abstract class and implements its abstract method 
class ClassFour extends ClassThree{
   public void methodThree(){
      System.out.println("This is methodThree");
   }
}

// Demonstrate results
class Result{
   public static void main(String args[]){
       ClassFour classFour = new ClassFour();
       classFour.methodThree();
   }
}

Output:

This is methodThree

But an interface can extend any number of interfaces at a time.

// First interface
interface InterfaceOne {
    public void methodOne();
}

// Second interface
interface InterfaceTwo {
    public void methodTwo();
}

// This interface is extending both the above interfaces
interface InterfaceThree extends InterfaceOne,InterfaceTwo {
}

// Define regular class that implements InterfaceThree 
class RegularClass implements InterfaceThree {
    public void methodOne(){
        System.out.println("This is methodOne");
    }
    public void methodTwo(){
        System.out.println("This is methodTwo");
    }
}

// Demonstrate results
class Result{
    public static void main(String args[]){
        RegularClass regularClass = new RegularClass();
        regularClass.methodOne();
    }
}

Output:

This is methodOne

Difference 2. What can extend

An abstract class can extend concrete (regular) class or abstract class.

// Define abstract class with regular method 
abstract class ClassTwo{
   public void methodTwo(){
      System.out.println("This is methodTwo");
   }
}

// Define abstract class that extends abstract class and introduces new abstract method 
abstract class ClassThree extends ClassTwo{
   abstract void methodThree();
}

// Define regular class that extends abstract class, 
// implements its abstract method and override regular method of ClassTwo
class ClassFour extends ClassThree{
   public void methodTwo(){
      System.out.println("This is methodTwo overridden in ClassFour");
   }

   public void methodThree(){
      System.out.println("This is methodThree");
   }
}

// Demonstrate results
class Result{
   public static void main(String args[]){
       ClassFour classFour = new ClassFour();
       classFour.methodTwo();
   }
}

Output:

This is methodTwo overridden in ClassFour

But interface can extend another interface only.

// First interface
interface InterfaceOne {
    public void methodOne();
}

// This interface is extending first interface
interface InterfaceTwo extends InterfaceOne {
}

// Define regular class that implements InterfaceTwo 
class RegularClass implements InterfaceTwo {
    public void methodOne(){
        System.out.println("This is methodOne");
    }
}

// Demonstrate results
class Result{
    public static void main(String args[]){
        RegularClass regularClass = new RegularClass();
        regularClass.methodOne();
    }
}

Output:

This is methodOne

Difference 3. Keyword "abstract" for methods

Keyword "abstract" is mandatory for declaring a method as an abstract in abstract class.

// Define abstract class with abstract method
abstract class ClassOne{
   abstract void methodOne();
}

// Define regular class that extends abstract class, 
// and implements its abstract method
class ClassTwo extends ClassOne{
   public void methodOne(){
      System.out.println("This is implemented methodOne");
   }

   public void methodTwo(){
      System.out.println("This is methodTwo");
   }
}

// Demonstrate results
class Result{
   public static void main(String args[]){
       ClassTwo classTwo = new ClassTwo();
       classTwo.methodOne();
   }
}

Output:

This is implemented methodOne

But in interface keyword "abstract" for methods is optional, because methods abstract in interface by default.

// First interface
interface InterfaceOne {
    public void methodOne();
}

// Define regular class that implements InterfaceTwo 
class RegularClass implements InterfaceOne {
    public void methodOne(){
        System.out.println("This is methodOne");
    }

    public void methodTwo(){
        System.out.println("This is methodTwo");
    }
}

// Demonstrate results
class Result{
    public static void main(String args[]){
        RegularClass regularClass = new RegularClass();
        regularClass.methodOne();
    }
}

Output:

This is methodOne

Difference 4. Access modifiers for methods

An abstract class can have protected and public abstract methods.

// Define abstract class with protected and public methods
abstract class ClassOne{
   protected abstract void methodOne();
   public abstract void methodTwo();
   public abstract void methodThree();
}

// Define regular class that extends abstract class, 
// and implements its methods
class ClassTwo extends ClassOne{
   public void methodOne(){
      System.out.println("This is methodOne");
   }

   public void methodTwo(){
      System.out.println("This is methodTwo");
   }

   public void methodThree(){
      System.out.println("This is methodThree");
   }
}

// Demonstrate results
class Result{
   public static void main(String args[]){
       ClassTwo classTwo = new ClassTwo();
       classTwo.methodOne();
   }
}

But interface can have public abstract methods only.

// First interface
interface InterfaceOne {
    void methodOne();
}

// Define regular class that implements InterfaceOne
class RegularClass implements InterfaceOne {
    public void methodOne(){
        System.out.println("This is methodOne");
    }

    public void methodTwo(){
        System.out.println("This is methodTwo");
    }
}

// Demonstrate results
class Result{
    public static void main(String args[]){
        RegularClass regularClass = new RegularClass();
        regularClass.methodOne();
    }
}

Difference 5. Variables

An abstract class can have static, final or static final variables with any access specifier.

// Define abstract class with variables
abstract class ClassOne{
   private int varOne = 1;
   protected int varTwo = 2;
   public int varThree = 3;
   public void methodOne() {
      System.out.println("varOne = "+varOne);
   };
}

// Define regular class that extends abstract class, 
// and introduces new method that use variables of abstract class
class ClassTwo extends ClassOne{
   public void methodTwo(){
      System.out.println("varTwo = "+varTwo);
      System.out.println("varThree = "+varThree);
   }
}

// Demonstrate results
class Result{
   public static void main(String args[]){
       ClassTwo classTwo = new ClassTwo();
       classTwo.methodOne();
       classTwo.methodTwo();
   }
}

Output:

varOne = 1
varTwo = 2
varThree = 3

But interface can have constant variables only (that implicitly public static final).

// First interface
interface InterfaceOne {
    int varOne = 1;
}

// Define regular class that implements InterfaceOne
class RegularClass implements InterfaceOne {
    public void methodOne(){
        System.out.println("varOne = "+varOne);
    }
}

// Demonstrate results
class Result{
    public static void main(String args[]){
        RegularClass regularClass = new RegularClass();
        regularClass.methodOne();
    }
}

Output:

varOne = 1

Resume

In Java abstract classes and interfaces used for introducing abstraction. But usage of these constructions has differences that should be take in count.


Read also:

Ways of creating threads in Java

Simple network console chat on Java

Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed