There are three main differences
between an interface and an abstract class:
- At the same time
multiple interfaces can be implemented, but only extend one class
- an abstract class may
have some method implementation (non-abstract methods, constructors,
instance initializers and instance variables) and non-public members
- abstract classes may or
may not be a little bit faster
The above differentiation suggests when to use an abstract class and when to use an interface:
- If you want to provide
common implementation to subclasses then an abstract class is used,
- If you want to declare
non-public members, the use abstract method
- In case of abstract
class, you are free to add new public methods in the future,
- If you're confirm
regarding the stablity of the API for the long run then use an interface
- If you want to provide
the implementing classes the opportunity to inherit from other sources at
the same time then use an interface.
0 comments:
Post a Comment