Class Design Abstract: Why Not Define Public Constructors?

Have a look here (Abstract Class Design): http://msdn.microsoft.com/en-us/library/ms229047.aspx

It says:

(1) Public or protected internal (protected friend in Visual Basic) constructors in abstract types should not be defined.

In C #, we cannot instantiate an abstract class. So, is it still important to define public constructors for abstract classes in C #? Or not write public constructors for abstract classes because of semantic meaning?

It also says:

(2) Define a protected or internal constructor in abstract classes.

Define internal constructors? It is stated in (1) that the non-definition of internal protected constructors is due to the fact that "constructors with open or protected internal visibility are for types that can be created." Does the internal constructors for abstract classes determine to break the rules in (1)?

Thanks in advance.:)

+4
source share
3 answers

Look at each case.

Recommended:

  • protected . The most obvious case is that all subclasses can call the constructor, no matter what assembly they are in (as long as the abstract abstract class itself).

  • internal Useful if you want the abstract type to be public, but not publicly inherited. In this case, you would like to create all non-private internal constructors. Only subclasses in the same assembly as the abstract base class could effectively call constructors, only they could inherit. Another use case would be if you needed a β€œspecial” constructor that should only be visible to subclasses of the same assembly.

  • private Used primarily for dirty constructors that are intended for other constructors of an abstract class when it uses a chain of constructors. The only other use when all the constructors are private is only to allow the subclass to be nested classes that have access to private members of the containing type.


Not recommended:

  • public . Not useful, behaves the same as protected . Only subclasses can invoke the constructor anyway, since the base class is abstract.

  • protected inside . This is no different from protected . Accessibility level protected internal means protected OR internal, not protected AND internal. However, the internal modifier has no purpose here - it does not prevent subclasses outside the assembly from calling the constructor (provided that the abstract base class is public), since they can rely on protected access and do not allow the same assembly types that do not are subclasses of the call (the type is abstract).

The key point here is that every constructor is not private in an abstract class is protected at best. The vanilla internal modifier reinforces the restrictions on who can call the constructor. public and protected internal do nothing, because they seem to ease the restrictions, but they really don't.

+6
source

n C #, we cannot create an instance of an abstract class. So, is it still important to define public constructors for abstract classes in C #? Or not write public constructors for abstract classes because of semantic meaning?

That's right. You do not want the user to see an accessible constructor, but when they call it, they get a compilation error.

Define internal constructors? It is stated in (1) that the non-definition of internal protected constructors is due to the fact that "constructors with open or protected internal visibility are for types that can be created." Does the internal constructors for abstract classes determine to break the rules in (1)?

I believe that rule 1 on public and protected internal rule 2 is protected and internal . Thus, there is no intersection between them.

0
source

Years passed. I think I’d better understand this question now. Therefore, I would like to add some more materials besides Ani's excellent answer.

In C #, we cannot instantiate an abstract class. So, it's still important to define public constructors for abstract classes in C #

It does not matter to the compiler, but it is important to read the code. Public constructors in abstract types are misleading code readers (they might think that they could be created).

Define internal constructors? It is stated in (1) that non-internal protected constructors are what are "Constructors with public or protected internal visibility for types that can be instantiated." Doesn't define internal constructors for abstract classes break the rules in (1)?

If we want an abstract class to be inherited only by subclasses within the same assembly, obviously we cannot use protected (otherwise it can be inherited outside the assembly). Now we have a choice:

  • public . As mentioned above, public is misleading to read code. Do not use it.

  • private We want the abstract class to be inherited by subclasses within the same assembly, and not just nested subclasses, so private will not work.

  • protected internal - we will never need it, because it is no different from protected .

  • internal is misleading to read code, but we have no other choice. I would have thought it was a compromise .

0
source

All Articles