Abstract class VS interface with properties and de...
# announcements
s
Abstract class VS interface with properties and default implementation which one is preferred by you and why?
p
Why does the interface need a property? Isn’t it meant to share behaviour description and not the state?
s
I cant see any reason for the abstract class so I would prefer the interface
a
both have their use-cases. an
abstract class
can have internal state (
private val/var
) and final methods
l
abstract classes are very different from interfaces, no?
I infer that you are asking from a design standpoint - I would venture to say "interfaces," but more information is needed
s
I have started with interface, then I found that I want to add some default implementation to it, after it I found that I have common property and I doing the same with it in any class that implements the interface so I thought to share this code and eventually it looks like abstract class but with public property and not protected as I can do with abstract class
s
Interfaces used to be "pure" abstract classes, but now with default implementations the lines are more blurred. The one difference that still remains between the two in your use case is that with a class, your properties can be initialized to a default value, while with an interface you'll have to "implement" the property in every implementing class. TL;DR an interface with default impls and properties pretty much IS an abstract class. Just use an abstract class.
g
Interface cannot hold any state