Search code examples
c#.netconstructorabstract-class

Relevance of 'public' constructor in an abstract class


Is there any relevance of a public constructor in an abstract class? I can not think of any possible way to use it, in that case shouldn't it be treated as error by compiler (C#, not sure if other languages allow that).

Sample Code:

internal abstract class Vehicle
{
    public Vehicle()
    {            
    }
}

The C# compiler allows this code to compile, while there is no way I can call this contructor from the outside world. It can be called from derived classes only.

So shouldn't it allow protected and private modifiers only?


Solution

  • There's no reason for a public constructor for an abstract class. I'd assume that the reason that the compiler doesn't complain is as simple that they just didn't spend time covering that since it really doesn't matter if it's public or protected.