JavaRush /Java Blog /Random EN /Closures and callbacks. Need some advice.
Konkistador
Level 28

Closures and callbacks. Need some advice.

Published in the Random EN group
In the chapter "Inner Classes" of B. Eckel's book on the topic "Closures and Callbacks" the following code example is presented: //innerclasses/Callbacks.java // Использование внутренних классов // для реализации обратных вызовов package innerclasses; interface Incrementable { void increment(); } // ПРОСТО реализуем интерфейс: class Callee1 implements Incrementable { private int i = 0; public void increment() { i++; System.out.println(i); } } class MyIncrement { public void increment() { System.out.println("Другая операция"); } public static void f(MyIncrement mi) { mi.increment(); } } // Если ваш класс должен вызывать метод increment() // по-другому, необходимо использовать внутренний класс class Callee2 extends MyIncrement { private int i = 0; private void increment1() { super.increment(); i++; System.out.println(i); } private class Closure implements Incrementable { public void increment() { // Указывается метод внешнего класса, иначе // возникает бесконечная рекурсия: Callee2.this.increment1(); } } Incrementable getCallbackReference() { return new Closure(); } } class Caller { private Incrementable callBackReference; Caller(Incrementable cbh) { callBackReference = cbh; } void go() { callBackReference.increment(); } } public class Callbacks { public static void main(String[] args) { Callee1 c1 = new Callee1(); Callee2 c2 = new Callee2(); MyIncrement.f(c2); Caller caller1 = new Caller(c1); Caller caller2 = new Caller(c2.getCallbackReference()); caller1.go(); caller1.go(); caller2.go(); caller2.go(); } } The compiler reports that it cannot override the increment() method in the Callee2 class. An attempt was made to assign lower access privileges. And this is understandable, in Java during inheritance it is possible to change access modifiers only in the direction of greater visibility. Tell me what the author wanted to convey? Where is the typo here?
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION