super::someMethod where someMethod is protected

Yuval Shavit yuval.shavit at gmail.com
Fri Dec 27 15:49:08 PST 2013


Apologies if this is a dupe. I searched, but my eyes have been known to
skip over things before...

Consider this silly example:

    public class Vehicle {
      protected String sayVroom(String in) {
        return "vroom: " + in;
      }
    }

    public class Car extends Vehicle {
      @Override
      protected String sayVroom(String in) {
        return Stream.of(in).map(super::sayVroom).toString();
      }
    }


If both classes are in the same class, this works fine. But if they're not,
I get a compilation error in Car:

    sayVroom(java.lang.String) has protected access in
com.yuvalshavit.sandbox.p1.Vehicle)

If I replace the method reference with an explicit lambda, it works fine:

    return Stream.of(in).map(s -> super.sayVroom(s)).toString();

This is on OS X, b120 (Dec 12).


More information about the lambda-spec-observers mailing list