What is the difference between abstract class and interface in Java?

The main difference between abstract class and interface in Java is that the abstract class is used to implement abstraction while interface is used to implement abstraction as well as multiple inheritance.

An important pillar in object-oriented programming (OOP) is abstraction. It is the process of hiding the implementation details and showing only the functionality to the user. For example, the user does not need to understand the circuitry within the remote to operate it. Abstraction is similar to that. It allows the programmer to focus on what the object does instead of how it is done. In Java, there are two methods to achieve abstraction: using abstract classes and using interfaces. Also, inheritance is another pillar of OOP. It refers to the use of data and methods of an existing class. Multiple inheritance refers to the use of data and methods from multiple classes. It is possible to implement multiple inheritance using interfaces.

Key Areas Covered

1. What is abstract class in Java?
     – Definition, Functionality
2. What is interface in Java?
     – Definition, Functionality
3. What is the difference between abstract class and interface in Java?
     – Comparison of key differences

Key terms

Abstract class, interface, multiple inheritance

What is abstract class in Java?

A class that is declared with the abstract keyword is known as an abstract class. An abstract class can have abstract methods as well as non-abstract methods. A class can extend an abstract class and implement the abstract methods of the abstract class. It is not possible to create objects from an abstract class, but it is possible to create objects from a class that extends the abstract class. See the example below.

                                                           Figure 1: Abstract Class Form

                                                                Figure 2: Rectangle class

                                                     Figure 3: Main class for program 1

Form is an abstract class. It has the drawing of the abstract method and the visualization of the non-abstract method. The Rectangle class extends Shape. Therefore, the Rectangle class implements the abstract method draw. In the main function, a Rectangle object is created. The obj.draw() will call the draw() method on the rectangle and print “Rectangle”.  

What is interface in Java?

An interface in Java is similar to a class. All methods in an interface are abstract methods. Similar to an abstract class, an interface cannot be instantiated. An interface can be used for the abstraction as well as for the implementation of multiple inheritance. An interface is declared using the interface keyword. A class that implements the interface must implement all the methods declared in the interface. See the following example that illustrates the use of the interface to achieve the abstraction.

                                                                 Figure 4: form interface

                                                               Figure 5: Circle Class

                                                     Figure 6: Main class of program 2

Shape is an interface, which has an abstract method called draw. The Circle class extends Shape. Therefore, the Circle class implements the abstract method draw. In the main function, a Circle object is created. The s.draw() page will call the draw method on the circle and print “Circle”.

Interfaces can also be used to implement multiple inheritance.

                                                                   Figure 7: Interface A

                                                                Figure 8: Interface B

                                                                     Figure 9: Class C

                                                       Figure 10: Main class of program 3

Interfaces A and B have the display abstract method. Class C implements interface A and class B. Class C provides the implementation for the method display. In the main method, a C object is created. The obj.display will call the C display method and print “Hello world!”.

Difference Between Abstract Class and Interface in Java

Definition

An abstract class is a class declared with an abstract keyword, which is a collection of abstract and non-abstract methods. In contrast, an interface in Java is a reference type that is similar to a class that is a collection of abstract methods. This is the basic difference between abstract class and interface in Java.

variables

The variables used are a difference between abstract class and interface in Java. While an abstract class can have final, non-final, static, and non-static variables, an interface can only have static and final variables.

Methods

Another difference between abstract class and interface in Java is that abstract class can have abstract methods and non-abstract methods while interface can only have abstract methods.

multiple inheritance

Also, abstract classes cannot be used to implement multiple inheritance. But interfaces can be used to implement multiple interfaces. Therefore, we can say that applicability is a difference between abstract class and interface in Java.

class keyword

Also, the abstract keyword is used to declare the abstract class, but the interface keyword is used to declare an interface. 

Extending and implementing

Also, an abstract class can be extended using the “extends” keyword. An interface class can be implemented using the “implements” keyword.

Implementation

Also, the abstract class can be implemented using keyword “implements”, while the interface is used to implement abstraction and multiple inheritance.

conclusion

The difference between abstract class and interface in java is that abstract class is used to implement abstraction while interface is used to implement abstraction as well as multiple inheritance.

Reference:

1. “Abstract Class in Java – Javatpoint.” www.javatpoint.com, available here.
2. “Interface in Java – Javatpoint.” www.javatpoint.com, available here.

Leave a Reply

Your email address will not be published.

CAPTCHA


Back to top button