May 14, 2015 05:38 PM|Rion Williams|LINK
Abstract Classes and Interfaces
Abstract classes will allow you to insert basic functionality and methods (implementation details) within your actual classes unlike Interfaces which will not (so abstract classes can ultimately reduce the amount of code that you may have to write).
If you want a more detailed discussion, I would recommend checking out the following exhaustive Stack Overflow discussion on the topic :
There really is no always use one or always use the other and it is almost completely up to the preference of the developer and the needs that he or she needs to make (most of these decisions will come from future inheritance as both of these are heavily
involved in being derived from).
If you needed to implement functionality that a ton of your classes would share such as a specific property or method and the implementation would be the exact same for all of them, then you would want to use an abstract class. However, if you wanted each
of the classes to have a method or property but allow each of them to provide their own implementation for it (similar to that of a blueprint) then an interface may be a better choice.
I'd strongly encourage you to check out some of the links provided below, which discuss when to use one over another like these :
As far as real world examples, anytime that you see an actual class that beings with an "I" typically denotes an interface. So things like IEnumerable, IQueryable and IConnection are all interfaces that have cooresponding implementations (Enumerable, Queryable,
A common example that you might read on several sites may sound as follows :
1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate
just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for
certain animals, like Fish, which does not breath the same as most animals).
2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.