Differences -
1. If new method added to interface all implementing classes needs to add that and give an implementation whereas its not the case with abstract class.
2. Class implementing an interface needs to implement all methods which is not the case with abstract classes.
3. An Interface can only have public members whereas an abstract class can contain private as well as protected members.
4. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
5. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.
6. Abstract class can have constructor which can be accessed by subclass.
Similarities -
1. Both can't be instantiated.
2. Fully abstract class and interface ares similar.
Which one to be used and when -
When we need implementing class to follow some rules in method signature, use interface. And when some common base functionality needs to be implemented then use abstract classes. Mostly interfaces are used when frequent design changes needs to be done.
No comments:
Post a Comment