chriptus13
chriptus13

Reputation: 717

Override internal method in another package

Suppose I have a base class like following.

package somePackage

open class Base {
    internal open fun foo() {
        println("base-foo")
    }
}

Shouldn't I only be able to call the internal method if in same package as Base?

Why does the following work?

package otherPackage

class SubClass : Base()

fun main() {
    SubClass().xpto()
}

As mentioned here it is possible to increase the visibility of a method. Although I'm not doing that, since I'm not overriding the method. If I do something like the following I understand but without overriding why does it allow me to call the internal method?

class SubClass : Base() {
    override fun xpto() {
        super.xpto()
    }
}

Upvotes: 0

Views: 514

Answers (1)

broot
broot

Reputation: 28352

internal visibility in Kotlin isn't the same as package visibility in Java. internal means that the element is visible to the code inside the same module, where module is described as the code compiled together, e.g. a subproject/module in Gradle/Maven. This is explained here: https://kotlinlang.org/docs/visibility-modifiers.html#modules

Upvotes: 2

Related Questions