Last post Jan 27, 2014 12:30 PM by Rion Williams
Jan 27, 2014 12:04 PM|bbcompent1|LINK
These two concepts are escaping me. I have read and read and it isn't making sense to me. In any case, I was wondering if anyone could explain the following:
What is an abstract class? Why use it? Same questions for an interface. Any real-world examples? I appreciate any help that can be offered. Thank you!
Jan 27, 2014 12:27 PM|AidyF|LINK
An abstract class is a class that can't be created, it can only be inherited from. An interface is simply a contract, a guarantee that the class that implements the interface will provide the given properties and methods. The functional difference between
an abstract class and an interface is that when you implement an interface you have to implement all of the methods etc yourself. When you inherit from an abstract class, the abstract class can implement methods and properties itself, leaving you to either
override the inherited implementation, or an abstract class can also provide no implementation meaning that the dervied class has to provide its own.
You might have an abstract Person class with Name and Address fields and a GetFullName() method. Your system might then provide classes that inherit from Person such as Employee, Manager, OfficeCleaner etc. These classes will all have name and address
functionality for free, as well as their own specific properties as your system might only deal with specific types of person.
Jan 27, 2014 12:27 PM|kedarrkulkarni|LINK
interface is must inherit that means, the implementing class must provide implementation of all methods of interface
in case of abstract class, it can contain functionality of some function...
example - we can say all collection objects like arraylist, list etc can be iterated over... thats because, they all implement IEnumerable.
see this recommendations about when to use abstract class and interface
hope this helps...
Jan 27, 2014 12:30 PM|Rion Williams|LINK
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
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,
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.