lannyf
lannyf

Reputation: 11025

kotlin, how to make a internal function also able to be override in a sub class (in other module)

android project has multiple modules. module A has some base class in kotlin

package xxx.module_a

open class InModule_A {
   protected function action() {...}
}

class Runner() {

    fun doSomething() {
        InModule_A().action(). // it is NOT compile, but if the action() is internal it is ok since they are in same module
    }

}

in module A the Runner class need to access the InModule_A() class member function action().

And the InModule_A.action() should only be visible inside the module A and to be overridden in its derived classes in other module.

In module B, it has class InModule_B derived from InModule_A.

package xxx.module_b

class InModule_B {

   protected override function action() {// if InModule_A().action() were a internal it would not be able to override here 
   
   super.action()
   ... ...
   }
}

how to make function has internal visibility and also to able to override in the derived class?

Upvotes: 0

Views: 993

Answers (1)

Tenfour04
Tenfour04

Reputation: 93609

I'm not 100% sure I understand, but maybe creating an alternate function that calls through to the protected function would fit your situation.

open class InModule_A {
    protected open fun action() {}
    internal fun internalAction() = action()
}

class Runner() {

    fun doSomething() {
        InModule_A().internalAction()
    }

}

Upvotes: 2

Related Questions