Constructors of an abstract class shouldn’t be public and they should be protected. My question is about methods in that abstract class. Can we declare them as public or they should be protected too for the same reason?
Constructors of an abstract class shouldn’t be public and they should be protected .
Share
The justification for constructors on
abstracttypes beingprotectedis that there is simply no other entity that could call the constructor other than a derived type. Making the constructorpublicis meaningless in this case as it can’t ever be invoked outside the type hierarchy. Hence the recommendation is to useprotectedas it’s the most appropriate access modifier.The same logic doesn’t hold true with other members on the type. They can be freely invoked from outside the type hierarchy should their access modifier permit it.
Whether or not a particular member should be
publicorprotectedis highly dependent upon the particular API. The reasoning should be the exact same forabstracttypes as it is for non-abstract types