Interface and Abstract class

Difference between abstract class and interface in Java 8

Brief Overview

Interface is one of the special feature of Java programming language. Many people Confuse between interface and inheritance , but they are completely different to each other .

Abstract class is a feature of Java (Object oriented Programming language).The definition of both the term have many similarities but they have significant difference between them.

First we discuss abstract class and interface separately with an example ,after we explore the difference between them.

Abstract Class

Abstract class contains only the declaration of the methods inside a class and not the definition in the main class. The sub classes .which are inherited by the parent class can have their own definition . The abstract class cannot create the objects.

The subclass methods must override the base class methods . Let us understand this with the following example :

Abstract Class Example

abstract class animal{
   
   public abstract void voice();
}
//dog class inherits  animal class
public class Lion extends Animal{

//Overriding the voice()  
 public void voice(){
	System.out.println("Roar");
   }
   public static void main(String args[]){
	Lion obj = new Lion();
	obj.voice();
   }
}

Output:

Roar.

In the above example ,the animal is the class ,we made it as abstract , we define a method known as voice() , Every animal has it’s own voice and it differ from one to another. There is no point in defining a voice method inside a animal class, hence we need to define the voice depending on which animal is inheriting from the main class.

Interface in Java

This is another way of implementing the abstraction in Java. Interface is defined by the keyword interface , it contains only the name of the methods witout any declaration. Yes it sounds similar to the abstract class ,but difference is there in between ,In next part of the section ,I will discuss.

Interface Example

interface  animal{
   
   public  void voice();
   public void sleep();

}
//dog class inherits  animal class
public class Lion implements animal{

//Overriding the voice()  
 public void voice(){
	System.out.println("Roar");
   }
public void sleep(){
	System.out.println("zzz");
   }
   public static void main(String args[]){
	Lion obj = new Lion();
	obj.voice();
        obj.sleep();
   }
}

Output:

Interface Program

Few important points for interface

  • Like abstract classes, interfaces cannot be used to create objects.
  • Interface methods do not have a implementation – the definition is provided by the implemented class
  • On implementation of an interface, we need to override all of its methods.
  • Interface methods are by default abstract and public
  • Interface attributes are by default publicstatic and final
  • An interface cannot define a constructor

The next part is the difference between abstract class and Interface.

The difference between Abstract Class and Interface

Abstract ClassInterface
 Abstract class can have abstract and concrete methods.Interface can have only abstract methods. In Java 8, it can have default and static methods .
Abstract class does not support multiple inheritance.
Interface supports multiple inheritance.

Abstract class contain final, non-final, static and non-static variables.
Interface can have only static and final variables.

The abstract keyword is used to declare abstract class.

An abstract class can extend another Java class and implement multiple Java interfaces.
The interface keyword is used to declare interface in Java.

An interface can extend another Java interface only not extending the class.
 A Java abstract class can have class members like private, protected, public.
Members of a Java interface are public by default.

Example:
public abstract class animal{
public abstract void voice();
}
public interface Write{
void draw();
}

Conclusion

In a nutshell , Interface achieve 100% abstraction but abstract class doesn’t. Means abstract class having limitation compared to interface in java. Depending on the situation we can use the interface and abstract class in our programs .

Java 8 support few new features compared to the old kava versions, in the above table ,we provided the difference is supporting the declaration of static and default methods.