Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 276
kamahl Avatar asked Feb 22 '23 04:02

kamahl


2 Answers

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.

like image 117
Jon Skeet Avatar answered Mar 03 '23 09:03

Jon Skeet


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.

like image 24
Dave M Avatar answered Mar 03 '23 08:03

Dave M