Hi guys...
I was wondering how you guys decide when to use abstract or interface class during the application development since they both provide similar functionalities with slightly difference. I appreciate any helps. Thanks.
Hi guys...
I was wondering how you guys decide when to use abstract or interface class during the application development since they both provide similar functionalities with slightly difference. I appreciate any helps. Thanks.
when i'm developing and trying to decide whether to use an interface or an abstract class i usually think about whether classes that will inherit will contain only the same structure (methods, properties etc) but different implementations.
if the implementation of the methods will be different, but i want to ensure sameness from a structural standpoint i use an interface. if the structure and implementation are the same i tend to use abstract classes.
I use abstract classes when I want the inheriting classes to inherit some functionality, and interfaces when I want to set some minimum structural criteria for a group of classes.
One thing to remember is that any given class can "inherit" (technically implement) many interfaces but only one sub-class (be that abstract or not).
Languages like PHP support multiple interface implementation but not multiple class inheritance, and this is often part of the decision -- will your class support multiple behaviors (interfaces) or will it act as one type of thing (base class)?
There is also the question of how the base functionality is implemented. If you are using an approach like the template method pattern where you have common implementation code for all derived classes, you will want to use an abstract base class.
Use abstraction if you have default methods (with accompanying code) for inheritors. Use interfaces if you just need to make sure that classes inheriting from this parent should implement all methods defined. Word of caution: use abstract classes and interfaces only if you intend to enforce structure and organization (usually with a team). There's a performance overhead.