kamahl
kamahl

Reputation: 941

Abstract Class: Single Constructor, access modifier?

I have the following code snippet:

public abstract class Foo
{
    protected Foo()
}

Is there any difference in inheritance between using protected as the access modifier or public?

I´ve mostly seen using protected in this case. So there is the difference and why do people use proteced over public?

Upvotes: 4

Views: 616

Answers (2)

Dave M
Dave M

Reputation: 1322

One difference is if you plan on serializing the class, then you must have a default public constructor (default meaning the constructor takes no arguments). Otherwise, like Jon says the difference is slight.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499810

There may be some subtle situations where it would make a difference (and it would be detectable with reflection) but essentially they're the same. It's probably clearer to make it protected, as it can't actually be called other than by a constructor of a derived class.

Upvotes: 6

Related Questions