locked
Why Abstract Class Required? RRS feed

  • Question

  • User-256025523 posted

    Can anybody tells me the significance of abstract class when we can achieve the the inheritance feature by normal class as well?

    Thanks,

    Prashant N

    Tuesday, December 29, 2015 11:42 AM

Answers

  • User-434868552 posted

    @pr_nimbalkar

    this question has been answered many times by many people ... you can find these answers using Google or your favourite search engine.

    Example

    http://programmers.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces

    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.

    edit:

    see also:

    http://www.c-sharpcorner.com/UploadFile/d0e913/abstract-class-interface-two-villains-of-every-interview/

    http://www.codeproject.com/Articles/6118/All-about-abstract-classes

    MSDN

    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"

    et cetera 

    end edit.

    edit # 2:

    N.B.:  frequently in interview questions, the real question is whether one should choose an abstract class or an interface.

    see http://forums.asp.net/post/4793832.aspx 

    end edit #2.

    edit #3:

    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:

    http://www.siteadvisor.com/sites/charlespetzold.com  

    end edit #3.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 1:41 PM
  • User753101303 posted

    Hi,

    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 https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx :

    "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 https://msdn.microsoft.com/en-us/library/system.data.common(v=vs.110).aspx

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 4:53 PM

All replies

  • User-434868552 posted

    @pr_nimbalkar

    this question has been answered many times by many people ... you can find these answers using Google or your favourite search engine.

    Example

    http://programmers.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces

    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.

    edit:

    see also:

    http://www.c-sharpcorner.com/UploadFile/d0e913/abstract-class-interface-two-villains-of-every-interview/

    http://www.codeproject.com/Articles/6118/All-about-abstract-classes

    MSDN

    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"

    et cetera 

    end edit.

    edit # 2:

    N.B.:  frequently in interview questions, the real question is whether one should choose an abstract class or an interface.

    see http://forums.asp.net/post/4793832.aspx 

    end edit #2.

    edit #3:

    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:

    http://www.siteadvisor.com/sites/charlespetzold.com  

    end edit #3.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 1:41 PM
  • User753101303 posted

    Hi,

    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 https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx :

    "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 https://msdn.microsoft.com/en-us/library/system.data.common(v=vs.110).aspx

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 4:53 PM