As you know, final (sealed in C#) classes are classes that cannot be derived. Therefore, it does not make sense to have protected methods on such classes, since the semantics of protected methods only come into play on derived classes (that are not possible on final classes, as mentioned above).
The class below has this problem:
final class MyClass
{
protected void foo() { ... }
}
Everything is fine, there is no damage done - Everything works as you would expect at runtime. It is just a little messy, since adding protected methods to final classes. This would indicate that some assumptions of the developer are wrong. Presumably the developer wanted, at some point, to be able to provide an overridden implementation of the method, but that is not possible because the class is final. The code makes promises that cannot be upheld.
The X++ compiler diagnoses the situation with a warning:
New protected method 'MyClass.foo()' declared in a final class.
<R>
{
for $c in /Class[@IsFinal='true']
for $m in $c/Method[@IsProtected='true']
return <ProtectedMethodsOnFinalClasses
Artifact='{$c/@Artifact}'
StartLine='{$m/@StartLine}' EndLine='{$m/@EndLine}'
StartCol='{$m/@StartCol}' EndCol='{$m/@EndCol}' />
}
</R>
One way of fixing is to make the method private, since there can be no methods overriding it. This would just be specifying the de facto semantics. However, you will not be able to do that if the method is derived from a protected one in a parent class, since restricting visibility is not possible.
*This post is locked for comments