0

Suppose following class Structure:

inside namespace a

public abstract class A {

    protected int someVal;
    protected abstract int action();

}

in namespace a.b

public class B extends A{

    //magically filled with elements ;)
    List<A> elements; 

    protected int action(){
        someVal = 42; // OK
        int l = 0;
        for (A a : elements){
            l+= a.action(); //FORBIDDEN!!
        }
        return l;
    }
}

so Eclipse suggests me to

Change the visibility of 'action' to 'protected'

I feel some kind of fooled.

Why I'm allowed to implement action, but not to call it? I have no problem accessing someVal either.

I'm well aware of copying B into the same namespace, but I have many Classes which extend B or similiar C's and D's etc. and like to have them in order.

Or to rephrase my Question: Is this packet 'thingy' the only way to force my classes to have a protected method, but hide that from client code?

Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • 1
    It's because `a.action()` is **abstract**, which typically means it's defined by the base class but isn't implemented until a descendant does so. There's no `base.action()` to access. – Ken White Jan 19 '13 at 02:51
  • What type of objects list A have?? – Renjith Jan 19 '13 at 02:52
  • @KenWhite I have a list of actual implementations of `A` which were all forced to implement `action()`. Therefore there can't be lack of that method – Rafael T Jan 19 '13 at 02:53
  • @Renjith is this really of matter? maybe B maybe C or CC which extends from C.... There are implementations of `A` – Rafael T Jan 19 '13 at 02:55
  • Actual implementations of `a` would imply a descendant which actually implements `a.action()`. An **implementation** would actually implement the **abstract** method by definition. Read again what I wrote in my comment. I think you need to read the documentation regarding what `abstract` means. :-) – Ken White Jan 19 '13 at 02:58
  • 1
    @KenWhite I think you are wrong! If i change `abstract` modifier of `action()` method, and return 0, I have the same error! As Eclipse states: this is an ACCESS Problem – Rafael T Jan 19 '13 at 03:03
  • 1
    Indeed, if `A.action` had default access ("package private"), then it wouldn't be implementable or overrideable in the different package of `B`. – Tom Hawtin - tackline Jan 19 '13 at 03:08

1 Answers1

2

From a static typing perspective a may not be an instance of B. You can't touch the protecteds of a non-descendent class (except via the same package get out). protected should be used rarely, if at all.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • I thought protected means 'beeing visible to this and subclasses or classes inside the package'. Why I have access to someVal which has the same keyword? – Rafael T Jan 19 '13 at 02:59
  • @RafaelIT - read that again. Note that B is not in/inside the package of A. – Stephen C Jan 19 '13 at 03:02
  • 1
    @RafaelT `someVal` is effectively `this.someVal`. `this` is statically an instance of `B`. (Similarly implementing `action` is on `this` (a `B`), whilst the `a.action()` is called on an `A`.) – Tom Hawtin - tackline Jan 19 '13 at 03:04