What is the difference between open and public in Kotlin?
What is the difference between open and public in Kotlin?
The main difference between open and public in Kotlin is:
public is a visibility modifier that allows a class, function, or property to be accessed from anywhere[1][2][3]. It is the default visibility if no modifier is specified.
open is not a visibility modifier, but rather a modifier that allows a class or member to be overridden in subclasses[1][2][4]. By default, classes, functions and properties in Kotlin are final and cannot be overridden, unless marked as open.
In summary:
public controls the visibility and accessibility of a declarationopen controls whether a class or member can be overridden in subclassesFor example:
open class Base {
open fun foo() { ... }
}
class Derived : Base() ...
middle