When to use abstract class and interface in java

When to use abstract class and interface in java is one of the most important topics in java. You can decide “when to use interface and abstract class” if you know the difference between abstract class and interface. If you are familiar with an abstract class and interface in java you can easily find the difference between them. But it’s not easy to decide when to use abstract class and interface in Java, because it depends upon the situation or how and what you want to do in your application. 

1. Common functionality

If you want to provide some common or default functionality then the abstract class would be a better choice. Because an abstract class can have abstract methods and concrete methods. So concrete method can provide some default behavior to each subclass.
On another hand, an interface can contain only abstract methods. Since java 8 we can define default methods in the interface, but the scope of methods is limited because it uses the default keyword.
We can take an example from JDK, here we have an abstract class AbstractCollection. The AbstarctCollection class provides common behavior to the subclasses like ArrayList, LinkedList, HashSet, etc.

when to use abstract class and interface in java with real time example

2. To make a contract

If you want to enforce some rule or want to make a contract of implementation the interface would be a better choice. An interface provides contracts only and it enforced the rule to subclass. So, it is the responsibility of subclasses to implement each and every single contract provided to them.

But an abstract class is not a good choice when you want to enforce some rule or want to make a contract of implementation. Let’s say you have two abstract classes with different contracts, but the concrete class can extend only one class. it can’t follow both the rule at the same time.

3. To support multiple inheritance

As you know we can’t achieve multiple inheritance by the use of classes. So, when you want to perform multiple inheritance then interface would be a good choice.
We can take the example of the creation of a thread. We have two ways to create a thread, those are by Extending the thread class and Implementing the Runnable interface.
If we are creating a thread by extending of thread class, then we can‘t extend any other class. But if we create an object by implement the interface then we can extend any other class also.

when to use abstract class and interface in java with real time example

when to use abstract class and interface in java with real time example

4. Access modifier

As know, we can declare the variable in the abstract class with different access modifiers. An abstract class doesn’t apply any restrictions. If you want to declare non- public variables while performing abstraction, then interface is a good choice. Because in the interface by default all variables are public.

In interface you can’t any variable other than public. All variables are by default public.

5. Related class or unrelated classes

Let’s say you want to use the abstraction, but you are getting confused about whether you should use the abstract class or interface. You can decide it on the base of its use if you need to provide functionality that is used by some type of class then use Abstract class. We can take the example of the AbstractCollection class. AbstractCollection provides the functionality which is used by same type class like ArrayList.

But if functionality can be used by completely unrelated classes than use the interface. Let’s take the example of a Cloneable interface. This interface is unrelated for each class but you can implement it and you the functionality.

6. Variable or final variable(Constant)

Suppose you have a situation in which you want to perform abstraction. You can’t tell the exact implementation of the method. You can use the abstract class or interface. But if you want to declare non-static or non-final variables then it can be possible only by the use of the abstract class.
Because in an interface we can declare only static and final variables.

when to use abstract class and interface in java with real time example

7. Without breaking existing code

An abstract class is a good choice when there is a chance to add more new behavior. Because you can still add behavior without breaking the existing code.
But in an interface, all the subclass must provide the implementation to the method.
Let’s say you had developed an application one year ago. You have been using the abstract class. If you want to provide some new behavior you can add it without breaking the existing code.
Since java 8, You can use the default method in interfaces to resolve this problem.
We recommend you, If you are designing small functionality, then interfaces are a good choice because can make changes after developed the functionality. But in large applications, it can create a problem.

8. Use abstract and interface together

We can use the abstract class and interface together. An interface can provide only a contract but by use of an abstract class, we can provide skeletal of that contract. We can take an example from the Collection frameworkIn the Collection framework, Collection is an interface that extends the Iterable interface and AbstractCollection is an abstract class that implements Collection interfaceAbstractCollection provides a skeletal implementation of the Collection interface. By use of this approach, it minimizes the effort to implement the Collection interface by concrete class.

Leave a Comment