Last Updated: 06 September, 2022
In order to achieve abstraction (hiding the internal implementation and only showing the functionality to the users) in Java, two ways have been provided: Abstract Class and Interface.
Let's first briefly understand Abstract Class and Interface, then we will move ahead with the differences between them.
A class that is declared with an abstract keyword is known as an abstract class in Java. An abstract class cannot be instantiated, which means we cannot create the object of an abstract class.
An abstract class may or may not contain abstract methods (methods that do not have a body, such as public void display();). But, if a class has at least one abstract method, then the class must be declared as an abstract class.
To use an abstract class, we have to inherit it from another class and provide implementations for the abstract methods in it. If an abstract method is not implemented, then that sub-class should be declared as ‘abstract’.
Syntax of Abstract Class
Read more Click here...
In Java, an interface is an abstract type that is used to specify a behaviour that classes must implement. An interface contains only method declarations (no method body) and constant variables without constructors and static methods.
Using the interface keyword, we can create interfaces in Java, but we cannot create objects of interfaces.
In Interface, all methods by default are public and abstract, and all variables by default are public, static, and final.
Syntax of Java Interface
Read more Click here...
Abstract Class | Interface |
---|---|
An abstract keyword is used to create an abstract class. | An interface keyword is used to create an interface. |
An abstract class can contain abstract as well as non-abstract methods. | The interface contains only abstract methods. Note: Since Java 8, an interface can have default and static methods as well. |
An abstract class can have final, static, and instance variables. | Interface has final and static variables only. |
An abstract class can provide the implementation of an interface. | An interface can't provide the implementation of an abstract class. |
We cannot achieve multiple inheritance using an abstract class. | Using an interface, we can achieve multiple inheritance. |
An abstract class can extend only one class, whereas it can implement many interfaces. | An interface cannot implement a Java class, but it can extend many interfaces. |
The extends keyword is used to extend an abstract class. | An interface can be implemented using a keyword implements. |
An abstract class achieves partial abstraction (0 to 100%). | Interface achieves full abstraction (100%). |
An abstract class can have constructors. | An interface cannot have constructors. |
An abstract class has protected and public abstract methods. | An interface can have only public abstract methods. |
Next, let's understand the implementation of the Abstract class and Interface.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com