Coffeehouse Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Abstract class vs Protected Constructors.

Back to Forum: Coffeehouse
  • User profile image
    magicalclick

    So, I am not sure why I want to do abstract class when I can just make my regular class.constructor a protected method. A regular class without public constructor is just like an abstract class. And with protected constructor, the sub-class can also say, public myClass2().Base() as constructor right? (syntax maybe wrong, but you get my point).

    The only difference is that public constructor in abstract base class will be available to sub-class without additional work? That's it?

     

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    spivonious

    Functionally, they're the same (at least as far as I can tell).

    Semantically, they're very different.

  • User profile image
    magicalclick

    that's good to know, thanks. I guess, it is just another, in-your-face kind of annotation to tell others that there is no public constructor available by itself.

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    cbae

    There's one very important difference. You can declare methods abstract in an abstract class, which forces all subclasses to implement the method. You can't do that with a concrete class. 

  • User profile image
    magicalclick

    that too, thanks for the tip.

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    AndyC

    Another is that you can do evil things with Reflection to call protected methods, but theres no way to subvert things to construct an abstract class.

  • User profile image
    Maddus Mattus

    @magicalclick: What prevents me from overriding the abstract class?

  • User profile image
    magicalclick

    @AndyC:

    that's good to know too. although not sure why anyone want to do that when they can also simply make a sub-class and call the protected methods? Both approaches want sub-classes anyway. Seems like some people does this and certainly have a good reason, but, why?

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    Charles

    Again, abstract classes are about more than object construction. A concrete class with a protected constructor is not an abstract class - or put another way, it's still a concrete class, as mentioned already. Use them for versionable components that also provide member-implementable types (this is what makes them versionable). See the thread on abstract class vs interface for more insights from Niners (and MSDN).

    C

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.