Reputation: 13303
Consider the problem:
We have a Base
class with an abstract method. Now, we would like to enforce that every override of this method will perform some argument checking or some other drudgery. We want this argument checking to be identical across all overrides. One solution would be to wrap this behavior in a non-abstract method which calls an abstract one:
abstract class Base
{
fun computeSomething(argument: Int): Int
{
require(argument > 0) // Some intricate checking
return execute(argument)
}
// Pure functionality, assuming correct arguments
// Ideally, this would be private.
protected abstract fun execute(validArgument: Int): Int
}
class Derived1: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument
}
class Derived2: Base()
{
override fun execute(validArgument: Int): Int =
validArgument * validArgument * validArgument
}
fun main(args: Array<String>)
{
val d = Derived1()
println(d.computeSomething(23))
}
I would like to have Base::execute
private, such that no subclass of Base
can call it by accident without checking its arguments. Unfortunately, this is not possible:
Error:(9, 5) Kotlin: Modifier 'private' is incompatible with 'abstract'
Kotlin's protected
is better than Java's protected
, since it makes the function accessible only to subclasses but still, the ideal case would be private
.
So is there some right way to implement this pattern? Also, just out of curiosity, is the incompatibility of private
and abstract
a deliberate choice of language design?
Upvotes: 5
Views: 1959
Reputation: 2167
You can make them to pass object that will check the value if they would like to call this method. example
abstract class Base
{
fun computeSomething(argument: Int): Int
{
return execute(ValueCheck(argument))
}
protected abstract fun execute(validArgument: ValueCheck)
protected class ValueCheck(a: Int)
{
private val value = checkValue(a)
private fun checkValue(argument: Int): Int
{
require(argument > 0)
return argument
}
fun get() = value
}
}
and when derived class would like to call execute, it have to pass object that is checking value when creating instance
Upvotes: 1
Reputation:
It doesn't make sense for a function to be overridden, but not callable from that class. By overriding that function, you own its implementation, and therefore can call it.
You can accomplish what you want by detaching the execution logic from the derived classes, which can be done in a few ways:
Base
By passing these to the Base
class in its constructor, you are transferring their ownership to that class.
Upvotes: 2