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.
One major pillar in Object Oriented Programming (OOP) is Abstraction. It is the process of hiding the implementation details and displaying only the functionality to the user. For example, the user does not need to understand the circuitry inside the remote control 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: by using abstract classes and by using interfaces. Moreover, inheritance is another OOP pillar. It refers to using data and methods of an already existing class. Multiple inheritance refers to using data and methods of 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. Refer the below example.
Figure 1: Shape abstract class
Figure 2: Rectangle class
Figure 3: Main class for program 1
Shape is an abstract class. It has the abstract method draw and non-abstract method display. 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 method in Rectangle draw() and it will 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 abstraction as well as for the implementation of multiple inheritance. An interface is declared by using the interface keyword. A class that implements the interface should implement all the methods declared in the interface. Refer the below example that illustrates the use of interface to achieve abstraction.
Figure 4: Shape 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() will call the method draw in Circle and it will 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
Interface A and B both have the abstract method display. Class C implements both interface A and B. Class C provides the implementation for method display. In the main method, an object of C is created. The obj.display will call the display method in C and prints “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 is one 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
Moreover, abstract classes cannot be used to implement multiple inheritance. But interfaces can be used to implement multiple interfaces. Therefore, we can say the applicability as a difference between abstract class and interface in Java.
Keyword of the Class
Also, the abstract keyword is used to declare the abstract class but the interface keyword is used to declare an interface.
Extending and Implementing
In addition, an abstract class can be extended using the keyword “extends”. An interface class can be implemented using keyword “implements”.
Implementation
Furthermore, abstract class is can be implemented using keyword “implements” whereas interface is used to implement abstraction as well as 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.
ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2Wr7TA0ZqarWWToa60v4yapZ1lmaPBpr7FmpqeZZmjequt1Zpm