I've run into a compiler error that doesn't quite make sense to me. I have an internal
property and I want to restrict its set
block such that it is only available through inheritance. I thought this would work:
internal bool MyProperty {
get { return someValue; }
protected internal set { someValue = value; }
}
But the compiler says that the access modifier on the set
block needs to be more restrictive than internal
- am I missing something, or is protected internal
not more restrictive than internal
?
Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class. The four access modifiers in Java are public, protected, default, and private.
Simply put, there are four access modifiers: public, private, protected and default (no keyword).
Modifiers in Java fall into one of two groups - access and non-access: Access: public , private , protected . Non-access: static, final, abstract, synchronized, volatile, transient and native .
Private members and attributes are completely hidden from outside classes as well as from subclasses. Protected access hides the class's methods and attributes from classes that exist outside of the class's package. This means that classes within the same package can access protected methods and attributes.
protected internal
is less restrictive; it is protected or internal (not and) - which therefore additionally allows subclasses from other assemblies to access it. You would need to invert:
protected internal bool MyProperty {
get { return someValue; }
internal set { someValue = value; }
}
This will allow code in your assembly, plus subclasses from other assemblies, get it (read) - but only code in your assembly can set it (write).
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