Last post Jul 08, 2014 01:32 AM by Rohit Binjola
Jul 06, 2014 08:12 AM|nitinsomal|LINK
Can we implement the abstract class on interface if yes then please provide the implementation
Jul 06, 2014 08:22 AM|Rion Williams|LINK
Abstract classes and interfaces contrast with regards to actual implementation and functionality (one contains implementation details and the other does not) so it would be quite difficult to use them both on the same class. Although this can depend
on exactly what you are trying to do and why you are trying to do it.
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
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.
Jul 08, 2014 01:32 AM|Rohit Binjola|LINK
No, we cannot do that.
As from the defination of Abstract class these classes are designed to be the base class and any common functionality that has to be used by many classes across the project needs to be written to the abstract class.
The major difference between interfaces and classes is that interfaces only provide signatures and not implementations. If the interface inheriting a class inherited its implementation, it would provide implementation and no longer be an interface.