Interface vs Abstract Class in Java: What’s the Difference?

Java is one of the leading and commonly used programming languages across the globe. It is an object-oriented programming language that can be run on any platform. Java programming language is based on the principle, WORA. WORA implies Write Once, Run Anywhere. In other words, Java language executes its compiled code anywhere from any platform without recompiling it. An abstract class and interface in java are the two significant classes. 

What is Java?

Java is a powerful programming language that ensures secure programming. The Sun Microsystem is the developer of the Java language. It acts as both a platform as well as a programming language. Developers can use Java programming language for developing mobile applications, desktop software applications, embedded systems, etc. 

The significant advantage of using the Java language is it is platform-independent. Developers can write and compile the Java code in one operating system or platform and run or execute the same compiled code on another operating system or platform. A Java Virtual Machine (JVM) is responsible for running any compiled Java code. To run any code on the Java Virtual Machine (JVM), the code must be converted into Java bytecode, as the JVM accepts on Java bytecode. 

Basic Language elements in Java

Java programming language is object-oriented. Hence, any Java code involves the combination of objects. These objects are used to call or invoke any function inside the code. Java programming language generally contains the following elements in its code:

  1. Class:

A class in Java programming language is a collection of methods and data variables. Additionally, the objects are created in the class 

  1. Object:

An object in the Java language represents the state, behavior, and identity of a particular thing. When you declare an object in the class, it is used to execute the same class’s methods. 

  1. Methods:

Methods are referred to as functions that execute a block of code for a specific purpose. Methods are called in the main function of any program using objects. 

  1. Instance Variables:

Instance Variables are unique variables that are assigned with each object type.

Below is the syntax of the Java program.

public class class_name
{
//declare variable
public static void main(String[] args)
{
class_name object_name= new class_name();
}
}

We have seen the class’s declaration and how to call it in its object’s main function. There are different classes in Java, like POJO class, static class, abstract class, concrete class, final class, and an inner class. However, in this article, we will learn only the abstract class. We have discussed the basics of Java. Let us now dive into our core topic, Abstract Class, and Interface in Java. 

READ  26 Best Mobile Application Development Tools in 2020

What is an Abstract Class?

A class that is declared using ‘abstract’ as a keyword is called an abstract class. Before we dive into an abstract class’s details, we shall first know what precisely the abstraction means. 

Abstraction

The word ‘abstraction’ implies not exposing or hiding the implementation methods, and only the functionality is displayed. In such a situation, the user is acquainted only with the necessary and essential things. There is no need for the user to know other internal features. An abstraction can be achieved using two Java elements – an abstract class and an interface. 

For example, A person sends a text message to the other person. The sender types the message and sends it. But, he or she is not aware of the process of how the message delivers to the other person. The sender knows the message and not the method of communication. This is the best example of abstraction. 

From the above instance, you might have understood the concept of abstraction. We shall now move towards the abstract class. Using the keyword ‘abstract,’ we can create an abstract class. 

Syntax:

abstract class class_name{}

The abstract class contains methods, which can be either abstract or non-abstract. 

What is the abstract method?

An abstract method is a function that does not have its implementation in it. It includes a function or method name followed by a semicolon.

Syntax:

abstract void method_name();

While implementing the abstract class, you need to remember some significant points. 

  1. You should always declare an abstract with an ‘abstract’ keyword.
  2. You can include abstract methods as well as traditional methods in your abstract class. 
  3. The most significant thing to remember is that you can’t instantiate the abstract class. In other words, you can’t create any object in the abstract class. 
  4. You can involve constructors and static methods in your abstract class. 
  5. Regular or traditional classes can’t have abstract methods. It only supports standard methods. 
interface vs abstract class

 Need for the abstract class in Java

When you use the abstract class, it only includes the functionality and not the implementation. But, subclasses of the abstract class utilize the functionality for implementation. In other words, subclasses can override the abstract class. Let us focus on an example to learn the concept of the abstract class.  

For example, consider a class Animal. We will include the functionality of animal sounds in the abstract class, i.e., we will take sound() as the method. Let us have Cat, Dog, and Lion as subclasses of the class Animal. Each animal has a unique sound. Hence, if we implement the sound() method in the superclass, it will be of no use. 

Every subclass should override the superclass for implementing the functionality defined in the superclass. Using the Animal as the abstract class, its every subclass, Cat, Dog, and Lion, will implement the sound() method. All subclasses of the superclass implement superclass’s methods using the keyword ‘extends.’

abstract class Animal
{
public abstract void sound();
}
public class Cat extends Animal
{
public void sound()
{
System.out.println("Meows");
}
}
public class Dog extends Animal
{
public void sound()
{
System.out.println("Woof");
}
}
public class Lion extends Animal
{
public void sound()
{
System.out.println("Roar");
}
}
class Animal1
{
public static void main(String args[ ])
{
Animal a1= new Cat();
Animal a2= new Dog();
Animal a3= new Lion();
a1.sound();
a2.sound();
a3.sound();
}
}

Output:

Meows
Woof
Roar

What is Interface?

Till now, we have covered the abstract class and abstract methods. We will move forward with the interface. What is an interface in Java? The purpose of using the interface and abstract class in Java is the same, i.e., to implement the concept of abstraction. 

READ  32 Most Common Interview Questions And Answers

An interface in Java has methods in it. But, these methods are abstract methods without any implementation. We use ‘abstract’ as a keyword to declare the abstract class. Similarly, the keyword ‘interface’ is used to declare the interface in Java. Like the abstract class, the interface only specifies the methods in it. And the classes implement the methods specified by the interface. 

Syntax:

interface interface_name { }

An interface in Java can contain methods without their implementation, and methods can be either public, static, or final. In the abstract class, classes implement the superclass’s methods using ‘extends’ as the keyword. In contrast, in the interface, classes that use the interface’s methods need to include ‘implements’ as the keyword. 

Need for the Interface in Java

One of the significant needs for using the interface in Java is to implement the multiple inheritance concept. In Java, the multiple inheritances are executed using interfaces. Another primary requirement for the interface in Java is for the abstraction. Both the interface and abstract class in Java follow abstraction. 

Why use two different concepts, the interface, and the abstract class, for implementing an abstraction? The reason for this question is the use of variables in the abstract class and the interface. We can use the final, public, and static variables in the interface while using only non-final variables in the abstract class. 

Let us now watch the example of the interface in Java. Consider the interface Shape with the side() as a method and classes Triangle, Square, and Pentagon. All three classes implement the side() method of the interface Shape. 

interface Shape
{
void side();
}
public class Triangle implements Shape
{
public void side()
{
System.out.println(" Three sides");
}
public class Square implements Shape
{
public void side()
{
System.out.println("Four sides");
}
}
public class Pentagon implements Shape
{
public void side()
{
System.out.println("Five Sides");
}
}
class TestShape
{
public static void main(String args[ ])
{
Shape t= new Triangle();
Shape s= new Square();
Shape p= new Pentagon();
t.side();
s.side();
p.side();
}
}

Output:

Three sides
Four sides
Five sides

Abstract Class vs Interface in Java

We have seen what are abstract classes and interfaces in Java. Also, we discussed the need for the abstract class and an interface in Java with their examples. Both the abstract class and an interface is used for implementing the abstraction in Java. However, further use of the interface in Java is for implementing multiple inheritance. The below table will assist you in understanding the exact difference between the abstract class and an interface in Java:

Abstract ClassInterface
An abstract class in Java includes abstract methods and traditional methods. An interface in Java strictly involves only abstract methods. 
The abstract class executes at a faster speed.An interface is relatively slower than the abstract class. 
It is used only for implementing the concept of abstraction and not the multiple inheritance. You can use an interface in Java for both abstraction and multiple inheritance. 
You can use static, non-static, and non-final variables in the abstract class. You can use the final, public, and static variables in the interface. 
The abstract class is used to avoid independence. The interface can be used for future enrichment. 
A single class can extend only one abstract class. A single class can implement several interfaces. 
The declaration of the abstract class uses the keyword ‘abstract.’ The interface in Java can be implemented using the keyword ‘interface.’
You can use access modifiers in the abstract class. An interface does not support the use of different access modifiers. Everything defined in the interface is public. 
The keyword ‘extends’ is used to extend the abstract class. The Keyword ‘implements’ is used to implement the interface. 
An abstract is flexible, which can extend another class and can implement several interfaces. An interface in Java can extend only other interfaces. 
You can declare constructors and destructors in the abstract class.Using constructors and destructors is not allowed in the Java interface. 
In the abstract class, you can define fields and constants. In the interface, you can’t define any field. 

All the above are the minor differences between the abstract class and the interface in Java. Notice all the differences properly between the abstract class and the interface and execute the codes appropriately. Apart from the abstract and the interface, there is concrete or traditional class in Java. 

READ  How to Hack Wi-Fi Network

What is Concrete Class in Java?

Unlike the abstract class and the interface in Java, the concrete class includes methods that have their implementations. You can create an object of the concrete class and instantiate it. Also, the concrete can implement the interface and extend the abstract class in Java. 

Syntax:

class class_name
{
constructor()
{
}
method1()
{
}
method 2()
{
}
.
.
.
}

The concrete class should have the implementation of all its methods. If any concrete class method does not have an implementation, the class is not the concrete class. The concrete class has a significant difference between the abstract class and the interface in Java. Now, let us know how the concrete class is implemented or executed with an example. 

Consider the class operation that has two methods, sum and difference. The concrete class contains the implementation of sum and difference methods, and the object can be created to invoke these methods. 

class Operation
{
static int sum (int a, int b)
{
return a+b;
}
static int difference (int a, int b)
{
return a-b;
}

public static void main (String args[ ])
{
int s = sum(10,7);
int d= difference(10,7);
System.out.println("Sum:", +s);
System.out.println("Difference:", +d);
}
}

Output:

Sum: 17
Difference: 3

Class Vs. Abstract Class Vs. Interface in Java

In this section of the post, we will see the difference between the traditional class, abstract class, and interface in Java. 

ClassAbstract ClassInterface
The class supports only concrete methods (methods having implementations) and not the abstract methods. The abstract class supports both abstract methods and concrete methods. But, the class that is declared as the abstract should have at least one abstract method.  The interface strictly supports only abstract methods and not concrete methods. 
In the concrete class, the method implementation is mandatory. In the abstract class of Java, it may or may not contain the method implementation. In the interface, there is no method implementation included. 
The concrete class does not use any keyword for declaration. The abstract class uses the ‘abstract keyword for declaration. The interface uses the ‘interface’ keyword for declaration. 
We can create objects of the concrete class. We cannot create objects of the abstract class, but the subclass implementing the abstract class can create objects. Like the abstract class, we cannot create objects in the interface. The class that implements the interface creates objects. 

Abstract Class and Interface Java Example Code

We have seen in detail the different examples of the abstract class and interface. We will now move towards a single Java example code that will illustrate both the abstract class and the interface. 

interface Shape
{
public void side();
}
class Hexagon implements Shape
{
public void side()
{
System.out.println("Six Sides");
}
}
abstract class Animal
{
abstract public void sound();
}
class Lion extends Animal
{
public void sound()
{
System.out.println("Lion Roars");
}
}
public class TestMain
{
public static void main( String args[ ])
{
Shape hs= new Hexagon();
hs.side();
Animal ls= new Lion();
ls.sound();
}
}

Output:

Six Sides
Lion Roars

The above code was the best abstract class and interface Java example.

FAQ

1. What is the purpose of the abstract class and the interface in Java?

We use an abstract class in Java to represent multiple subclasses’ typical behavior without implementing the whole class. An interface in Java is primarily used to implement multiple inheritances, as Java is not compatible with multiple inheritances. Another use of the interface in Java is communication. 

2. What is Interface vs. Abstract Class?

An abstract class in Java enables users to define functionalities, and its subclasses implement these functionalities. On the other hand, an interface in Java only defines the functionalities but does not implement them. Any traditional or concrete class can extend only one abstract class, whereas one class can implement several interfaces. 

3. Do the abstract class and interface in Java have a constructor?

The abstract class can have constructors in it, whereas the interface does not include constructors. It only has abstract methods. 

Conclusion

Java is one of the majorly used programming languages, which is platform-independent. It offers the flexibility of running the compiled java code on any other platform with ease. In Java, the abstract class and the interface are introduced to implement the abstraction concept. In the abstraction, any method’s implementation details are hidden from the user. 

In Java, the abstract class and the interface have a minor difference between them. An abstract class in Java has a minimum of one abstract method, whereas the interface strictly has the abstracts methods in it. The abstract method does not involve the implementation. We have also seen Abstract class Vs. Interface. Later, we covered the concrete class and class vs. abstract class vs. interface. We have noted down the individual and a combined Java example of the abstract class and interface. 

Recommended Articles