Difference Between Abstract Class and Interface in Java
In this article, we will touch upon the topics “abstract class” and “interfaces” in Java and later look at the differences between both concepts. We will begin by looking at one of the pillars of OOPS called abstraction, then briefly recap on abstract classes and interfaces and why we need them and see an example code for each.
In the end, we will differentiate both of them by using a table and then finish off by discussing when to make use of which concept. The table will show the key differences between the two concepts, such as their definition, purpose, and when they are typically used. The discussion will focus on the benefits and drawbacks of each concept, as well as the factors that should be considered when choosing one over the other.
What do you mean by the concept of abstraction in Java?
We are not going to dream of touching on the English definition of this word, so, in the computer science world, Abstraction is the process of hiding the details of how something works and only showing the user what they need to know to use it.
This can be done in many ways, such as through metaphors, icons, or menus. Abstraction makes it easier for users to understand and use complex systems, as they do not need to know all the details about how the system works to use it effectively.
For example, when you use a computer, you do not need to know how the hardware or software works to use it. You can simply click on icons or type in commands, and the computer will do the rest. This is because the operating system abstracts away the details of how the computer works and only shows you the information that you need to know to use it.
Abstraction is a powerful tool that can make complex systems easier to understand and use. It is used in many fields, such as computer science, engineering, and mathematics.
Now that we know what abstraction is, let us look at 2 methods of performing abstraction in Java.
What do you mean by abstract class in Java?
An abstract class is a class that cannot be instantiated. It is declared with the keyword “abstract.” Abstract classes can have abstract and non-abstract methods. Abstract methods are methods that do not have a body.
Abstract classes are like blueprints for other classes, but they cannot be created themselves. Like regular classes, they can have constructors to initialize values. They can also consist only of regular methods.
An abstract class can have a final method, but an abstract method cannot be final. In other words, a final method cannot be abstract because this would cause an error.
An abstract class is a class that cannot be instantiated but can be inherited from it. A subclass cannot override a final method. If an abstract method were also final, it would be impossible to override it, making it pointless to be abstract in the first place.
Abstract classes can have static methods, and any class with at least one abstract method must be declared abstract. If a child class cannot implement all of the abstract methods of its parent class, then it should be declared abstract itself so that a subclass can provide implementations for the remaining abstract methods.
Before we move on and look at what interface is, let us take a look at an example program to get a better picture of abstract classes:
abstract class FirstCode {abstract void showdetails();} class FirstCodeEmployee extends FirstCode { void showdetails() { String name = "Gopi Kiran"; int age = 20; int ID = 21213; System.out.println("COMPANY: FirstCode"); System.out.println("NAME: "+name); System.out.println("AGE:"+age); System.out.println("ID: "+ID); } } class Main { public static void main(String args[]) { FirstCode emp1 = new FirstCodeEmployee(); emp1.showdetails(); } }
Output
COMPANY: FirstCode
NAME: Gopi Kiran
AGE: 20
ID: 21213
In the program shown above, the class “FirstCode” is an abstract class, meaning it cannot be instantiated. It has one abstract method, “showdetails()”, which must be implemented by any class that inherits from it. The class “FirstCodeEmployee” inherits from “FirstCode” and overrides the “showdetails()” method to provide its implementation.
What is meant by an interface in the context of Java?
An interface in Java is a blueprint or a contract that specifies the methods that a class must implement. It serves as a method to attain abstraction in the Java programming language. Interfaces provide a common interface to a group of classes.
An interface in Java is a blueprint or contract that defines the methods that a class must implement. It cannot have a method body, only abstract methods and variables.
Interfaces are used to achieve abstraction and multiple inheritance in Java. In other words, an interface can define the functionality that a class must provide, but it does not specify how that functionality is implemented.
interface FirstCode { final int a = 10; void display(); } class FirstCodeEmployee implements FirstCode { public void display() { String name = "Gopi Kiran"; int age = 20; int ID = 21213; System.out.println("COMPANY: FirstCode"); System.out.println("NAME: "+name); System.out.println("AGE:"+age); System.out.println("ID: "+ID); } } class Main { public static void main(String[] args) { FirstCodeEmployee emp1 = new FirstCodeEmployee(); emp1.display(); } }
Output:
COMPANY: FirstCode
NAME: Gopi Kiran
AGE: 20
ID: 21213
In the program shown above, the class “FirstCode” is an interface. It has one method, “display()”. The class “FirstCodeEmployee” inherits from “FirstCode” and overrides the “display()” method to provide its implementation.
What are the differences between interface and abstract class?
We can see that both interfaces and abstract classes are used to perform abstraction. They are very similar, so similar that we have written the same code using both concepts. However, there is more to these concepts than meets the eye.
Let us take a look at the differences between them in a good old fashion tabular method:
PARAMETER | INTERFACE | ABSTRACT CLASS |
Performance | Interfaces are slow | Abstract classes are fast |
Inheritance | Can inherit numerous interfaces | Can inherit only one abstract class |
Implementation | It can be a tedious task to find all the implementers and implement newly defined stuff when adding new stuff to the interface. | Abstract classes provide a default implementation of methods, which derived classes can use. |
Access specifier | Interfaces do not have access modifiers; rather, everything defined within an interface is assumed to be a public specifier. | Unlike interfaces, abstract classes can have any access specifiers. |
Parameters | An interface cannot contain data fields | An abstract class can have any number of data fields |
Constructors | An interface cannot have constructors or destructors | An abstract class can include constructors or destructors within its structure. |
When to use which concept?
Now that we know the differences between both concepts, let us consider when to use which.
When to use abstract classes?
Abstract classes are used when multiple implementations of the same type share a common behavior. This means that you can create a class with all of the common methods and properties that all of the different implementations will need. Then you can create different classes that inherit from the abstract class and provide their implementations of the methods and properties.
When to use interfaces?
An interface is the better option when different implementations share only the method signature. This is because an interface is a contract that defines the methods that must be implemented by a class.
This allows for polymorphism, which is having multiple objects of different types respond to the same message. Interfaces can also be used to define the behavior of a data type without worrying about who implements it.
Using Abstract Classes and Interfaces Together
Combining interfaces and abstract classes is the best way to design a system. For example, in the JDK, java.util. A list is an interface that contains many methods. To make development easier, there is an abstract class java.util.AbstractList provides a basic implementation for all the methods of the List interface. This allows any subclass to extend this class and implement only the required methods.
It is best to start with an interface as the foundation and define methods every subclass must implement. Then, if there are methods that only certain subclasses should implement, we can extend the base interface and create a new interface with those methods.
Subclasses can choose to implement either the base interface or the child interface, depending on their requirements. If the number of methods grows large, it is not a bad idea to provide a skeletal abstract class that implements the child interface, giving the subclasses the flexibility to choose between an interface and an abstract class.
Conclusion
In Java, abstract classes and interfaces both contribute to the concept of abstraction, enabling developers to hide implementation details and focus on defining behavior. Abstract classes serve as blueprints with shared methods and properties for multiple implementations, offering a common base class.
Interfaces, conversely, provide a contract specifying methods a class must implement, promoting flexibility and polymorphism. The choice between abstract classes and interfaces depends on design requirements, for example, abstract classes are suitable for shared behavior, while interfaces excel in scenarios where different implementations share only method signatures.