Anil Philip wrote:I agree about 'protected' having greater visibility, but I don't see why one can say that 'protected' has wider access.
In the derived class DerivedService, it has lost access to the package members of com.example.a
I don't think it ever
had access, so I wouldn't say it lost anything.
Anil Philip wrote:In Stefan's example, if from the body of com.example.b.Service.protectedMethod(), (just suppose) it could access not only everything in DerivedService and also com.example.b (as expected),
but also everything in the original package com.example.a - then it has not only kept access that base class Service had, but also gained additional access to com.example.b and then one can say that 'protected' has wider access.
Perhaps it is a misnomer?
So, there
is no com.example.b.Service.protectedMethod(). I guess you mean, if we put one there, and if we were calling other stuff from that method. But that's beside the point. It sounds like you're thinking that if a method is protected, it might confer more power to access stuff outside that method. That's backward. A modifier like "protected" on a method does not affect what
that method can see or access. Rather, it affect whether
outside code can see or access that method. That's why Stefan's code is testing what can be seen or accessed from
other locations, namely com.example.a.SamePackage and com.example.b.DerivedService, which are testing their ability to access com.example.a.Service.protectedMethod() and access com.example.a.Service.packagePrivateMethod().