I'm reading the book "C# Language", and hit this note from Vladimir Reshetnikov:
If a static class declares a protected or protected internal member, a compile-time error occurs (CS1057).
May I know why? What's wrong with a static class having a protected member? Static class can have private member so I guess this CS1057 error is not due to accessibility, but maybe it's due to come compilation issue? as protected member could be overridden in child classes... but I couldn't figure out why.
Since Static classes are sealed classes, and sealed classes can not be inherited,hence it can not contain protected members, because protected members can not be inherited.
One of the important property of java constructor is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.
Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.
Core Java bootcamp program with Hands on practiceYes, we can have private methods or private static methods in an interface in Java 9.
Because you can't inherit a static class, protected
serves no purpose - only public
and private
make sense here.
More details can be found here: Why can't I inherit static classes?
Protected members means they can be accessed from child/derived classes. But the main features of static class are:
Only contain static members;
Can't be instantiated;
Are sealed.
That's why static classes can't have protected members.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With