The Java recognizes the limited nature of abstract classes and interfaces and their inability to become an object or a traditional class. Nevertheless, it is the absence of the qualities above and the ability to use abstract classes in various objects that proves to be incredibly useful to developers. In this article, I briefly outline the possible application of abstract classes and interfaces as well as provide examples to highlight the contrast between them.
“An abstract class is a class where some methods are abstract, by having no body. Implementations for these methods will be provided in extending subclasses.” (Havelund and Roşu, 2002).
Let’s illustrate the advantages of abstract class on an example. Let’s say a company hired a group of engineers to create a new kitchen robot that automates tasks such as meal preparation, serving, refrigerating and dish cleaning. The group of developers needs to implement a class that supports following methods:
The developer group has just finished the prepare_meal() method, when the business announced that due to lack of funding they will suspend the project. To make sure that all remaining methods are captured, developers outlined them as abstract methods in an abstract class called: Kitchen_Robot (Jarosciak, 2018a). Important to note is, that all Java classes that includes abstract methods must also become abstract.
Jarosciak, J. (2018a) Abstract Class Kitchen_Robot. Unpublished figure
In 2018, the company renewed its effort to complete the project and hired a new group of engineers who salvaged the previous abstract class by creating a new Kitchen_Robot_2018 class that extends Kitchen_Robot (Jarosciak, 2018b). Because Java object cannot be instantiated from an abstract class, the engineers extended the original class and completed work by overriding the outstanding abstract methods instead.
Jarosciak, J. (2018b) Extending Kitchen_Robot class. Unpublished figure
As we can see, the abstract class not only allowed original engineers to outline all kitchen robot features, but it also provided the foundation for future who developers to complete the project (Jarosciak, 2018c).
Jarosciak, J. (2018c) Complete Kitchen_Robot class. Published at: https://code.sololearn.com/cgfOR6bjMuTU
The Code Diagram (Jarosciak, 2018d).
Jarosciak, J. (2018d) Kitchen_Robot diagram
“Abstract interfaces are a powerful part of the Java language and are used to implement critical elements of the Java API.” (Farley, 1998).
Interfaces are very similar to abstract classes. They are also a way to achieve abstraction in Java, albeit with a couple of critical differences. Interfaces can only contain static variables and abstract methods and cannot have constructors. Same as abstract classes, they cannot be instantiated.
The following diagram shows the implementation of Robot interface and implementing classes (Jarosciak, 2018e).
Jarosciak, J. (2018e) Robot Interface diagram. Unpublished figure
The abstract interface is an entirely abstract class, but it’s unique by the ability to include only abstract methods. Note: The ability to create a concrete method by using ‘default’ keyword is available in Java 8.
”Abstract classes and interfaces are used to define the hot spots and allow the framework to specify methods that the user has to implement.” (De Neuter et al., 2007).
In a situation where we have standard features to be shared by all objects, developers typically use abstract classes. Java abstract classes may or may not contain concrete and abstract methods, as well as constructors, however, those cannot be called directly as abstract classes cannot be instantiated. Interfaces, on the other hand, are used when features are unique and different. The advantage of interfaces is that class can implement any number of interfaces and interfaces can extend other interfaces. The primary role of abstract classes and interfaces is to organize the code better and make it more logical and readable.
De Neuter, B., Luts, J., Vanhamme, L., Lemmerling, P., & Van Huffel, S. (2007). Java-based framework for processing and displaying short-echo-time magnetic resonance spectroscopy signals. Computer methods and programs in biomedicine, 85(2), 129-137.
Farley, J. (1998). Java distributed computing. ” O’Reilly Media, Inc.”.
Havelund, K., & Roşu, G. (2002, April). Synthesizing monitors for safety properties. In International Conference on Tools and Algorithms for the Construction and Analysis of Systems (pp. 342-356). Springer, Berlin, Heidelberg.