Both abstract classes and interfaces facilitate polymorphism but we see their differences here and then move on to examining valid use cases for each.
1. Abstract classes may not be instantiated, this for a fact, holds true for interfaces as well.
2. Abstract classes may have implementations for some of their methods, but interfaces have no method implementations at all.
So, that's essentially one difference only. But this critical difference also governs their suitability and applicability in programming contexts.
1. Use abstract classes if you anticipate some level of versioning for your components. To explain briefly, if you anticipate that your implementations will all have newer features tomorrow, use an abstract class for it may be updated to correspond to the newer functionality. Interfaces, on the other hand, once created cannot be updated and you will have to create a whole new interface.
2. Use abstract classes if you believe there will be some level of identical functionality among all implementations of your component. This is because, the common functionality may be implemented in the abstract class method whereas, interfaces do not support any implementations at all.
3. It's a good idea to use an abstract class to provide common functionality among all implementations of an interface. One manifestation of this idea lies in the abstract classes of Java Collections.
No comments:
Post a Comment