Interface vs Abstract Class in Java

By | May 2, 2022
Interface vs Abstract Class in Java

Java is a class-based, general-purpose, and object-oriented programming language. It follows multiple programming paradigms, including imperative, concurrent, generic, and functional. Also, it is a high-level and secure programming language primarily used as a server-side language for back-end development projects.

Data abstraction is one of the concepts of object-oriented programming. It is a property that emphasizes displaying only the essential details of an object and hiding trivial details. In Java, we can achieve abstraction using an abstract class and an interface.

Though an abstract class and interface share a similar purpose of achieving abstraction in Java, they have a lot of differences. Well, this article will help you understand the key differences between an abstract class and an interface in Java.

So, let us get started!

What is Interface in Java?

An interface is one of the ways to achieve abstraction in Java. It is analogous to a class in Java since it has data variables and methods. However, the methods declared in an interface are abstract, i.e., they do not have a body. Therefore, we can say that an interface in Java is a collection of abstract methods.

Another reason for using an interface in Java is that Java does not support multiple inheritance using classes. However, we can achieve multiple inheritance in Java using interfaces.

Interfaces specify what a class should do and not how it should do. A class that implements an interface inherits all the abstract methods from that interface. But if a class does not provide method bodies for the methods that it inherited from an interface, the class must be an abstract class.

Vamware

Syntax:

The following is the syntax of an interface in Java:

Vamware
interface interface_name
{
//declare constant fields
//declare abstract methods
}

We declare an interface using the ‘interface’ keyword. When we declare an interface, all the methods in it are public and abstract, i.e., having no body by default. Also, all fields in it are final, static, and public. A class implements an interface using the ‘implements’ keyword.

Moreover, one interface can extend another interface, which is analogous to a class extending another class. An interface extends another interface using the ‘extends’ keyword. A single interface can extend one or more interfaces, through which we can achieve multiple inheritance.

Advantages of Interfaces

The following are significant benefits of interfaces in Java:

  • Using interfaces, we can implement multiple inheritance in Java.
  • It helps a Java program to be loosely coupled.

Disadvantages of Interfaces

The following are some drawbacks of interfaces in Java:

  • It does not provide an implementation of a method at all.
  • An interface cannot define instance variables.

When to Use an Interface in Java?

You can use an interface in Java in the following scenarios:

Vamware
  • If your problem needs to be solved using multiple inheritance and consists of different class hierarchies.
  • When you need to specify the behavior of a data type but are not concerned about who implements its behavior.

Example

import java.io.*;
interface Animal{
void sound();
}
class Dog implements Animal{
public void sound(){
System.out.println("Dog Barks");
}
}
class Cat implements Animal{
public void sound(){
System.out.println("Cat Meows");
}
}
class Lion implements Animal{
public void sound(){
System.out.println("Lion Roars");
}
}
public class BaseAnimal{
public static void main(String args[]){
Animal ba=new Dog();
ba.sound();
Animal ba1=new Cat();
ba1.sound();
Animal ba2=new Lion();
ba2.sound();
}
}

Output:

Dog Barks
Cat Meows
Lion Roars

In the above example, we have created an interface Animal and an abstract method void sound();. There are three classes, namely Dog, Cat, and Lion that implement the interface, i.e., all of them implement the abstract method, void sound(); of the interface Animal.

What is an Abstract Class in Java?

In C++, we call a class an abstract class if it has at least one pure virtual function. Unlike C++, we can define a class as an abstract class using the ‘abstract’ keyword in Java. As an interface, an abstract class is also a mechanism to achieve abstraction in Java.

An abstract class in Java can have abstract methods (methods with no body) and non-abstract or concrete methods (methods with their implementation). More interestingly, we can create an abstract class without any abstract method.

Furthermore, we cannot instantiate an abstract class directly in Java, i.e., we cannot create the instance of an abstract class. However, we can subclass an abstract class, i.e., other classes can extend an abstract class.

Syntax:

The following is the syntax of an abstract class in Java:

abstract class class_name
{
//variables
//abstract methods or concrete methods
}

Advantages of Abstract Classes

The following are some notable benefits of abstract classes in Java:

  • Abstract classes in Java are useful in writing shorter codes.
  • They enable code reusability.
  • Abstract classes are faster than interfaces.

Disadvantages of Abstract Classes

The following are some significant downsides of abstract classes in Java:

  • We cannot instantiate an abstract class.
  • Abstract classes do not support multiple inheritance in Java as interfaces do.

When to Use an Abstract Class?

You can use an abstract class in Java in the following scenarios:

  • If you want to have non-static and non-final methods to modify the state of an object.
  • When the classes that extend an abstract have several fields and methods in common.
  • When we have specific requirements but only partial implementation details.

Example

import java.io.*;
abstract class Animal{
public abstract void sound();
public void sleep(){
System.out.println("Zzzzzz");
}
}
class Dog extends Animal{
public void sound(){
System.out.println("Dog Barks");
}
}
class Cat extends Animal{
public void sound(){
System.out.println("Cat Meows");
}
}
class Lion extends Animal{
public void sound(){
System.out.println("Lion Roars");
}
}
public class BaseAnimal{
public static void main(String args[]){
Dog g=new Dog();
g.sound();
g.sleep();
Cat c=new Cat();
c.sound();
c.sleep();
Lion l=new Lion();
l.sound();
l.sleep();
}
}

Output:

Dog Barks
Zzzzzz
Cat Meows
Zzzzzz
Lion Roars
Zzzzzz

We have implemented the same example as we did using an interface. Here, we have an abstract class Animal with one abstract method, void sound();, and one concrete method, void sleep(). Also, we have three classes, namely Dog, Cat, and Lion, that extend the abstract class Animal and provide method body for the abstract method void sound();.

Interface vs Abstract Class in Java

The following table draws the key differences between interfaces and abstract classes in Java:

Parameters Abstract Classes Interfaces
Types of Methods An abstract class can have both abstract and non-abstract methods. An interface can only have abstract methods. However, from Java 8 onwards, an interface can also have default and static methods.
Multiple inheritance Abstract classes in Java do not support multiple inheritance. We can achieve multiple inheritance in Java using interfaces.
Types of Variables An abstract class can have final, non-final, static, and non-static variables. An interface can only have final and static variables.
Implementation Abstract classes can provide the implementation of an interface. Interfaces cannot provide the implementation of an abstract class.
Declaration We declare an abstract class using the ‘abstract’ keyword. We can declare an interface using the ‘interface’ keyword.
Multiple Implementation An abstract class in Java can extend other abstract classes and implement multiple Java interfaces. An interface in Java can only implement the other Java interface.
Accessibility of Members Members of an abstract class can be private or protected. Members of an interface are public by default.
Extends and Implements We can extend an abstract class using the ‘extends’ keyword. We can implement an interface using the ‘implements’ keyword.
Access Modifiers An abstract class and its members have access modifiers. There is no need to specify access modifiers for an interface since its members are public by default.

Interface vs Abstract Classe – Which One to Choose?

Abstract classes and interfaces in Java can be used to achieve data abstraction. Though both are the mechanisms for achieving data abstraction, they are far different from each other. The above table highlights how an interface and an abstract class in Java differ from each other.

The primary difference between an abstract class and an interface is that the interface can achieve complete abstraction, whereas the abstract class can achieve partial or complete abstraction.

When multiple implementations share the single method signature, it is ideal to use an interface. On the flip side, an abstract class is an excellent choice when multiple implementations of the same kind share a common behavior.

Conclusion

Abstraction is the most significant aspect of the object-oriented programming paradigm, and Java provides two mechanisms to achieve it, namely an abstract class and interface. An interface is a collection of abstract methods, whereas an abstract class can contain abstract and non-abstract methods. Therefore, abstract classes can provide partial and complete abstraction, whereas interfaces can provide complete abstraction only.

Hopefully, this article helped you explore and understand the key differences between an abstract class and an interface in Java. Still, if you have any doubts or want to share any additional information regarding this topic, feel free to comment in the comments section below.

People are also reading:

Author: Ramya Shankar

A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing.

Leave a Reply

Your email address will not be published.