Protected Constructors For Abstract Classes

One code modification Resharper 4.0 will suggest is that if a public constructor is located in an abstract class, it should be adjusted to use the protected access modifier if currently public:

So, from this:

[csharp]
public abstract class AbstractClass
{
public AbstractClass()
{
}
}
[/csharp]

To this:

[csharp]
public abstract class AbstractClass
{
protected AbstractClass()
{
}
}
[/csharp]
So, why is this a good code edit?

Well, having a public constructor on an abstract class in the terms of code architecture is irrational since the abstract class can’t be instantiated directly; it is instead created by instantiating the deriving type. Thus only derived types have access to the abstract class constructor. Protected more adequately describes a germane access modifier for the constructor as well as ensuring requisite inheritor types have constructor access.

More noticeably, appropriate access modifier decoration will in turn not populate Intellisence with a bunch of useless crap :)

Share

4 thoughts on “Protected Constructors For Abstract Classes”

  1. I just don’t see why this is important. What are the relevant arguments for an implementation across a whole product for this…improvement.

Leave a Reply

Your email address will not be published. Required fields are marked *