Interface vs Abstract Class

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

What exactly is an interface?
An implementation of a class must adhere to the interface, which serves as a kind of blueprint for doing so. The interface does not include any concrete method implementations (methods that have code). Every method that makes up an interface is considered an abstract method.

It is not possible to instantiate an interface. On the other hand, classes that are able to instantiate interfaces are called implementor classes. Interfaces never have instance variables, but they can have public static final variables and other types of variables (i.e., constant class variables)

What Is Abstract Class?

The term “abstract class” refers to a class that has the abstract keyword included in its declaration. It is recommended that abstract classes have at least one abstract method, which are methods that do not have a body. It is possible for it to have several concrete methods.

Developing concrete classes is made easier when you use abstract classes as a template. However, the inheriting class ought to carry out the actions of the abstract method.

Interface Vs. Abstract Class

An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.

Parameters Interface Abstract class
Speed Slow Fast
Multiple Inheritances Implement several Interfaces Only one abstract class
Structure Abstract methods Abstract & concrete methods
When to use Future enhancement To avoid independence
Inheritance/ Implementation A Class can implement multiple interfaces The class can inherit only one Abstract Class
Default Implementation While adding new stuff to the interface, it is a nightmare to find all the implementors and implement newly defined stuff. In case of Abstract Class, you can take advantage of the default implementation.
Access Modifiers The interface does not have access modifiers. Everything defined inside the interface is assumed public modifier. Abstract Class can have an access modifier.
When to use It is better to use interface when various implementations share only method signature. Polymorphic hierarchy of value types. It should be used when various implementations of the same kind share a common behavior.
Data fields the interface cannot contain data fields. the class can have data fields.
Multiple Inheritance Default A class may implement numerous interfaces. A class inherits only one abstract class.
Implementation An interface is abstract so that it can’t provide any code. An abstract class can give complete, default code which should be overridden.
Use of Access modifiers You cannot use access modifiers for the method, properties, etc. You can use an abstract class which contains access modifiers.
Usage Interfaces help to define the peripheral abilities of a class. An abstract class defines the identity of a class.
Defined fields No fields can be defined An abstract class allows you to define both fields and constants
Inheritance An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces.
Constructor or destructors An interface cannot declare constructors or destructors. An abstract class can declare constructors and destructors.
Limit of Extensions It can extend any number of interfaces. It can extend only one class or one abstract class at a time.
Abstract keyword In an abstract interface keyword, is optional for declaring a method as an abstract. In an abstract class, the abstract keyword is compulsory for declaring a method as an abstract.
Class type An interface can have only public abstract methods. An abstract class has protected and public abstract methods.

It is not possible to instantiate abstract classes.

Important Considerations Regarding the Implementation of Interfaces

Interfaces are the means by which abstraction can be accomplished.
Created to support dynamic method resolution while the programme is running.
You can achieve loose coupling with the help of this.
This feature enables you to decouple the definition of a method from its parent inheritance structure.
Considerable Arguments in Favor of Employing Abstract Classes

Abstract classes are designed to provide subclasses with functionality by default.
Provides a template for future specific classes
Provides assistance in defining a standard user interface for its subclasses.
The ability to reuse code is provided by abstract classes.
Java source code examples for the Interface and the Abstract Class
The following is an example of Java code that can be used to create an interface and an abstract class.

Sample code for Interface and Abstract Class in Java

Java Interface Example: name of the interface / Methods of the Interface

class Dog implements the Pet interface, which consists of a public void test() call, and the Pet interface () system.out.println(“Interface Method Implemented”); public static void main; (String args[]) The abstract class syntax looks like this: Pet p = new Dog(); p.test();

abstract class name{ / code }
Abstract class example:

abstract class Shape { int b = 20; abstract public void calculateArea(); }

extends is a public method on the Rectangle class. void public static shape main void public (String args[]) { Rectangle obj = new Rectangle(); obj.b = 200; obj.calculateArea(); } public void calculateArea() { System.out.println(“Area is ” + (b * b)); } }