In kotlin when apply "internal" to the member function of a public class, it is only visible inside the module.
In the case that there are core module, and another module (call it outermodule) which has class derived from the class defined in the core module.
core module
package com.core
class BaseClass {
internal fun method_internal() {...}
public fun method_public() {...}
}
in the core module, the method_internal()
can be accessed outside the BaseClass
.
In the app whoever has dependency on the core module, the BaseClass
can be used in app, but in the app it cannot see the internal method_internal()
. That is the internal
behavior wanted.
In another module (the outermodule) it has class which derived from the BaseClass
outermodule
package com.outermodule
class DerivedClass : BaseClass {
......
}
in the outermodule it can use the method_public()
from DerivedClass,
but cannt to access the method_internal()
.
And cannot make the method_internal
as protected
since it should allow access in everywhere in the core module.
How to make the method has internal
visibility in one module, but at least be able to accessed from derived class in other module?
CodePudding user response:
It ain't pretty, but you can create two functions.
open class BaseClass {
protected fun foo() {
println("foo!")
}
internal fun fooInternal() = foo()
}