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.
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
Abstract class, interface, multiple inheritance
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”.
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!”.
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.
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.
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.
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.
Also, the abstract keyword is used to declare the abstract class, but the interface keyword is used to declare an interface.
Also, an abstract class can be extended using the “extends” keyword. An interface class can be implemented using the “implements” keyword.
Also, the abstract class can be implemented using keyword “implements”, while the interface is used to implement abstraction and multiple inheritance.
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.
1. “Abstract Class in Java – Javatpoint.” www.javatpoint.com, available here.
2. “Interface in Java – Javatpoint.” www.javatpoint.com, available here.
Main Difference - Summary vs Conclusion Summary and conclusion are two terms that are often…
Difference between moth and butterfly fall into two categories: anatomical and behavioral. Most moths are…
An engineer is a person whose job is to design and build engines, machines, roads,…
Internet is the term used to identify the massive interconnection of computer networks around the…
A CD-R is a type of disc that does not contain any data. It is blank…
Computing technologies are constantly evolving, and if we base our predictions on Moore's Law, they…