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 are some disadvantages of Kotlin?

expert

How Kotlin coroutines are better than RxKotlin/RxJava?

junior

Where should I use var and where val ?

Bình luận

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

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