Abstract class: Single Constructor, access modifier?

I have the following piece of code:

public abstract class Foo
    protected Foo()


Is there a difference in inheritance between using protected as an access modifier or public?

Mostly I've seen use of protected in this case. So, is there a difference and why do people enjoy protection over the public?


source to share

2 answers

There may be some subtle situations in which it will make a difference (and it will show up with reflection), but they are essentially the same. This is most likely to be done protected

because it cannot be called anything other than a derived class constructor.


One difference is that if you plan to serialize the class, then you must have a default constructor public

(by default, this means that the constructor takes no arguments). Otherwise, as John says, the difference is small.



All Articles