Câu hỏi phỏng vấn Kotlin
Câu hỏi

What is the difference between open and public in Kotlin?

Câu trả lời

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 declaration
  • open controls whether a class or member can be overridden in subclasses

For example:

kotlin Copy
open class Base {
    open fun foo() { ... }
}

class Derived : Base() ...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is the purpose of Unit-returning in functions? Why is VALUE there? What is this V ALUE ?

middle

val mutableList vs var immutableList . When to use which in Kotlin?

expert

What is the difference between launch/join and async/await in Kotlin coroutines?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào