Abstract Class Vs Interface in Java With Examples

Abstract class and interface are two methods that are used to achieve abstraction in Java. The two methods are used to keep the internal details hidden and to only show the functionality to the users. The methods help to eliminate the complexities of viewing the codes and facilitate the reuse of the codes. The detailed differences between abstract class vs interface are given below.

What is Abstract Class?

An abstract class is a class that is declared in Java with an abstract keyword. The abstract class may or may not contain all the abstract methods.

It can also have concrete methods in place of abstract methods. However, there is the absence of an object in the abstract class so it cannot be directly instantiated.

If a class includes at least a single abstract method, then it should be declared as an abstract class. A default constructor is always there in an abstract class and there may also sometimes be the presence of a parameterized constructor.

“Abstract class is a class that usually contains at least one abstract method”

A few of the interfaces are implemented by using it. To make it useful, an abstract class should be inherited from another class. The abstract class can include an abstract method, fields, main method, data member, method body, and constructor.

Here, an abstract method refers to the method declared without implementing it. To create an abstract method, the abstract keyword is placed before the name of the method and the abstract method must be redefined in the subclass. While declaring an abstract method, curly brackets are not used. Instead, semi-colons are used and such method only contains a signature, not a body.

A method in the parent class is declared as an abstract method by using the abstract keyword in the situation in which the coder desires a class to include a particular method but at the same time wants the real implementation of that method to be fixed by the child class.

The blueprint of a class that is used to get Java abstraction is known as an interface. Abstract methods, constants, nested types, static methods, and default methods are included in an interface so it is also referred to as a collection of abstract methods.

Example:

What is Interface?

An interface is an abstract type that is used to specify a behavior that classes must implement. In another word, it is a collection of abstract methods. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations. Both abstract class and interface are used in Java to achieve abstraction but an interface cannot be instantiated like an abstract class.

An IS-A relationship is represented by an interface and all the methods contained in it must be defined in the class unless the class is an abstract class. The behavior of an interface is similar to that of a class and both the class and interface are written in the same way. A single interface can be further extended to various other interfaces and it is related to capabilities.

An interface only specifies the function of a class but not specifies the way the class needs to perform its function. Besides the main usage of the interface to achieve abstraction, it can also be used to achieve loose coupling. The functionality of multiple inheritances is not supported by a class but an interface supports it.

“Interface contains only abstract methods”

An abstract class is declared by using the abstract keyword and an interface is declared by the use of interface keyword. After declaring an interface by the keyword, it is implemented by using the implements keyword.

An interface helps to achieve total abstraction in Java because all the methods in an interface are public and are defined with an empty body. In an interface, all of the fields are final, public, and static. Furthermore, the class that implements the interface must implement all the methods defined within the interface.

The main function of both the abstract class and the interface in Java is to help in achieving abstraction in Java and both the class and interface cannot be instantiated. However, there are a few differences between them which are explained below.

Example: 1

Example: 2 – Implementing multiple interfaces

Abstract Class Vs Interface

Basis of differences

Abstract class

Interface

Type of method

An abstract class may include both abstract and non-abstract method.

An interface can only include abstract methods and since the Java 8, it can also include static and default
methods.

Supporting multiple inheritance

An abstract class does not support multiple inheritance.

On the other hand, an interface supports multiple inheritance.

Type of variables

An abstract class can contain both final and non-final and both static and non-static variables.

However, an interface can only contain the final and static variable, not the non-final and non-static
variable.

Final variables

As mentioned in the above point, an abstract class may contain non-final variables.

But the variables that are defined within an interface are all final variables by default.

Implementation

An abstract class can be used to implement an interface.

An interface cannot be used to implement an abstract class.

Declaration

An abstract keyword is used to declare an abstract class.

An interface keyword is used to declare an interface.

Multiple implementations

An abstract class is able to extend
another class and can also further implement various other interfaces.

An interface only has the capacity to extend another interface.

Accessibility of data members

An abstract class contains both private and protected data members.

But only public data members are included in an interface.

Inheritance vs. Abstraction

An abstract class is extended by using the extends keyword.

An interface is implemented by using
the implements keyword.

Related posts: 

String to int in Java 

IO Streams in Java

Deadlock in Java

Garbage Collection in Java

Multithreading in Java

Leave a Reply

Your email address will not be published. Required fields are marked *