Last post Dec 29, 2015 04:53 PM by PatriceSc
Dec 29, 2015 11:42 AM|pr_nimbalkar|LINK
Can anybody tells me the significance of abstract class when we can achieve the the inheritance feature by normal class as well?
Dec 29, 2015 01:41 PM|gerrylowry|LINK
this question has been answered many times by many people ... you can find these answers using Google or your favourite search engine.
An abstract class is a partially defined class that cannot be instantiated. It (usually) includes some implementation, but leaves some functions as pure virtual- declared only by their signature. Pure virtual functions are not defined
in the class that declares them, so they must be implemented by a subclass (unless it too is an abstract class). Only a subclass which defines all of the pure virtual functions can be instantiated. The purpose of an abstract class is to define some common
behavior that can be inherited by multiple subclasses, without implementing the entire class. In C#, the abstract keyword designates both an abstract class and a pure virtual method.
https://msdn.microsoft.com/en-us/library/sf985hc5.aspx "abstract (C# Reference)"
"Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must
be implemented by classes that derive from the abstract class"
edit # 2:
N.B.: frequently in interview questions, the real question is whether one should choose an abstract class or an interface.
end edit #2.
pr_nimbalkar, the problem with questions like yours is that c# can not be taken in very tiny bites (sic); for that reason you are better off to take time to study books that
show you a more complete picture of how everything fits together.
Charles Petzold's 2006 book in just one example; it's free here: http://www.charlespetzold.com/dotnet/index.html
read the entire book. in Chapter 19, Virtuality Petzold shows the use of an abstract class, pp. 187 ff.
search free c# books for more choices.
be careful, if you are not sure about whether you can trust a website, check it out first, example:
end edit #3.
Dec 29, 2015 04:53 PM|PatriceSc|LINK
No. Don't look at what can be done the same way with two different features. See rather the doc and see what is the unique benefit for each feature. From
"They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented."
So in short, unlike a regular class an abstract class allows:
- to get a compile time error if a 3rd party developer tries to use your class as is. The error message likely tells that you have to inherit from this class
- and similarly you can require the 3rd party developer to implement a particular method you were not able to provide (and again with a compile time error)
You could perhaps do that with regular classes but at least you would need to throw runtime exceptions to let know the developer about the missing implementation parts (and one by one only when his code tries to use it). You likely won't use that often but
its nice to have when needed (so when you want to provide a base class without being able to provide a full implementation and having to let a 3rd party developer to provide the implementation for the missing parts).
For example in .NET it is used for db access classes found in
A 3rd party developer can then easily inherit from those classes and see which methods needs to be implemented to create a provider for his own database system. And for some other method, a default implementation is already provided so that you don't have
to be concerned about that. You have just to deal with what is particular to how your database works.