An interface is a 100% abstract class, so we can use an interface for efficient programming. Is there any situation where an abstract class is better than an interface?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
In general, interfaces describe the public API that your code should use, whereas abstract base classes are best kept as an implementation detail, where common code or state can be kept, to reduce duplication in any implementing classes.
By using interfaces in your API, it becomes easier for people (including you) to write test code against your classes, since you can use test classes that, for example, don't depend on any external resources, or which exhibit explicit kinds of bad-but-difficult-to-simulate-in-real-life behaviour.
So java provides the List interface, and the AbstractList abstract base class to "minimize the effort needed to implement" the interface...
There a couple of reasons why you might prefer an implementation-free abstract class over an interface:
But on the other hand, the interface Java keyword allows cleaner source.
Yes, there is a place for both abstract classes and interfaces.
Let's go with a concrete example. We'll look into how to make a
CheckingAccount
andSavingsAccount
from an abstractAbstractBankAccount
and see how we can use an interface differentiate the two types of accounts.To start off, here's an abstract class
AbstractBankAccount
:We have the account balance as
balance
and two methodsdeposit
andwithdraw
that must be implemented by the subclasses.As we can see, an abstract class declares the structure of how bank accounts should be defined. As @Uri mentions in his response, there is a state to this abstract class, which is the
balance
field. This would not be possible with an interface.Now, let's subclass
AbstractBankAccount
to make aCheckingAccount
In this subclass
CheckingAccount
, we implemented the two abstract classes -- nothing too interesting here.Now, how could we implement
SavingsAccount
? It is different from aCheckingAccount
in that it will gain interest. The interest could be increased by using thedeposit
method, but then again, it's not as if the customer is depositing the interest him/herself. Therefore, it might be more clearer if we had another means of adding money into an account, specifically for interest, say, anaccrueInterest
method.We could directly implement the method in
SavingsAccount
, but we may have more bank account types that can accrue interest in the future, so we may want to make anInterestBearing
interface that has theaccrueInterest
method:So, we now can make an
SavingsAccount
class that can gain interest by implementing theInterestBearing
interface:Now, if we want to make another type of account, say a
PremiumSavingsAccount
, we can make a subclass of theAbstractBankAccount
and implement theInterestBearing
interface to make another interest-bearing account.The
InterestBearing
interface can be seen as adding a common feature to different classes. It would have not have made sense to have a feature to deal with interest in a checking account when it doesn't accrue any interest.So, there are indeed places for both abstract classes and interfaces to coexist and work together in one situation.
Abstract class v/s interface is one topic that generates lot of curiosity/interest/confusion for anyone new to Java and wants to dig deeper.
This article provides a detailed explanation on the topic.
Abstract classes are used when you do intend to create a concrete class, but want to make sure that there is some common state in all the subclasses or a possible common implementation for some operations.
Interfaces cannot contain either.