Well, you could change your classes to be able to write something along these lines:
public static void main(String[] args) {
ClassB b = new ClassB("Example", new ClassA(() -> System.out.println("Hello")) {
@Override
public void action() {
}
});
}
But for this you would need to change the constructor of ClassA
to accept some implementation of a functional interface, like java.lang.Runnable
:
public abstract class ClassA {
public ClassA(Runnable runnable) {
runnable.run();
}
public abstract void action();
}
class ClassB {
public ClassB(String text, ClassA a) {
//Do stuff
}
}
Of course you still need to implement the abstract method in ClassA
, see the example above.