Can not call a private method, no idea why ?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Jan 21 11:20:20 UTC 2021


I agree with this analysis. This has nothing to do with default methods, 
and interfaces in general:

$ cat Test.java
import java.util.function.Function;

class Test {
   private void greet() { System.out.println("hello"); }

   void test(Function<? super Object, ? extends Test> f) {
     f.apply(null).greet();
   }
}

This still fails. As Anna points out, the function returns `? extends 
Test`, so the compiler cannot assume that what will be returned by the 
function will be "exactly" a Test. If it's a subclass, then the method 
will be inaccessible, even for the VM.

Maurizio

On 21/01/2021 10:53, Anna Kozlova wrote:
> Hi Remi,
>
> type of  `map.apply(null)` is capture of `? extends IOBug` and thus it 
> has no access to private methods of IOBug. Do I miss something?
>
> Thanks,
> Anna
>
> On Thu, Jan 21, 2021 at 9:34 AM Remi Forax <forax at univ-mlv.fr 
> <mailto:forax at univ-mlv.fr>> wrote:
>
>     The following code doesn't compile.
>     No idea why ?
>
>     public interface IOBug {
>       private IOBug foo() {
>         return this;
>       }
>
>       default IOBug flatMap(Function<? super Object, ? extends IOBug>
>     map) {
>         return map.apply(null).foo();
>                                ^------
>       }
>     }
>
>     It works if foo is declared as a default method instead of a
>     private method.
>
>     regards,
>     Rémi
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210121/429fcf81/attachment.htm>


More information about the compiler-dev mailing list