[Java] interface vs abstractClass
java, think, interface, abstractClass ·Goal
To get an advice for choosing between interface with default methods and abstractClass
The thing is
As I’m using default methods in interfaces, they seems not to have any differences. So I became wonder, which one to use? If I change from interfaces to abstract classes, won’t it be matter?
Differences
Still, there were differences.
-
interface
- Java allows only one “extends” from class.
- You can develop using interface, though there is not a clear hierarchy.
-
abstractClass
- You can use the states of the object, Field variables.
Dicision
As I didn’t want to occupy the only chance for extending, and there were not clear hierarchies, I decided to use interfaces. Now, the problem is accessing to the state. In interface, there were no way to force a object to holds certain states. I can try forcing some getter methods but its purpose may look vague for the other developers. I think this happened because I referenced many python codes, where multi inheritance is available. And I also found saying there is a way to serve both interface and abstractClass for user’s convenience, but for now, I think that’s too much.