
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to Use an Abstract Class and When to Use an Interface in Java
An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods.
If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java.
When to Use an Abstract Class
A Java class declared using the "abstract" keyword is called an abstract class. We cannot instantiate an abstract class, but we can extend it. We can use an abstract class in the following scenarios:
- An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
- It is also good if we want to declare non-public members. In an interface, all methods must be public.
- If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
- If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new interface.
- Abstract classes have the advantage of allowing better forward compatibility. Once clients use an interface, we cannot change it; if they use an abstract class, we can still add behavior without breaking the existing code.
- If we want to provide common, implemented functionality among all implementations of our component, use an abstract class. Abstract classes allow us to partially implement our class, whereas interfaces contain no implementation for any members.
Example: Using Abstract Class
In the following Java program, we are creating a Car class with an abstract method. Now, any other class that wants to extend it must implement the abstract method.
abstract class Car { public void accelerate() { System.out.println("Do something to accelerate"); } public void applyBrakes() { System.out.println("Do something to apply brakes"); } public abstract void changeGears(); } class Alto extends Car { @Override public void changeGears() { System.out.println("Implement changeGears() method for Alto Car"); } } class Santro extends Car { @Override public void changeGears() { System.out.println("Implement changeGears() method for Santro Car"); } } public class Main { public static void main(String[] args) { // object of Alto Car alto = new Alto(); alto.accelerate(); alto.applyBrakes(); // calling abstract method alto.changeGears(); // object of Santro Car santro = new Santro(); santro.accelerate(); santro.applyBrakes(); // calling abstract method santro.changeGears(); } }
Executing the above code will display the following result:
Do something to accelerate Do something to apply brakes Implement changeGears() method for Alto Car Do something to accelerate Do something to apply brakes Implement changeGears() method for Santro Car
When to use an Interface
In Java, an interface is a collection of abstract methods. Like an abstract class, it is also used to achieve abstraction. However, it is used in different cases, which are as follows:
- If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes.
- Interfaces are a good choice when we think that the API will not change for a while.
- They are also good when we want to have something similar to multiple inheritance, since we can implement multiple interfaces.
- If we are designing small, concise bits of functionality, use interfaces. If we are designing large functional units, use an abstract class.
Example: Using Interface
Following is an example demonstrating the usage of an interface in Java.
Nowadays, most of the actors are rich enough to produce their own movies. If we are using interfaces rather than abstract classes, we can implement both Actor and Producer. Also, we can define a new ActorProducer interface that extends both.
interface Actor { void perform(); } interface Producer { void invest(); } // interface extending other interfaces interface ActorProducer extends Actor, Producer { void createMovie(); } // class implementing interface class FamousActor implements ActorProducer { @Override public void perform() { System.out.println("The actor is acting on screen."); } @Override public void invest() { System.out.println("The actor is investing money in movie production."); } @Override public void createMovie() { System.out.println("The actor is producing and starring in his own movie."); } } public class Main { public static void main(String[] args) { // object ActorProducer actorProducer = new FamousActor(); actorProducer.perform(); actorProducer.invest(); actorProducer.createMovie(); } }
On running, the above Java program will display the following output:
The actor is acting on screen. The actor is investing money in movie production. The actor is producing and starring in his own movie.