Consider the problem:
We have a Base class with an abstract method. Now we would like to ensure that every redefinition of this method performs some argument checking or some other work. We want this argument to be identical in all overrides. One solution would be to wrap this behavior in a non-abstract method that calls abstract:
abstract class Base { fun computeSomething(argument: Int): Int { require(argument > 0)
I would like to have Base::execute private, so no subclass of Base can call it accidentally without checking its arguments. Unfortunately this is not possible:
Error: (9, 5) Kotlin: The modifier 'private' is incompatible with 'Abstract'
Kotlin protected better than Java protected , as it makes the function available only to subclasses, but the ideal case would nevertheless be private .
So, is there a proper way to implement this template? Also, out of curiosity, is the incompatibility of private and abstract deliberate choice of language design?
source share