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

How would you create a singleton with parameter in Kotlin?

middle

What is the idiomatic way to deal with null able values, referencing or converting them?

junior

How to correctly concatenate a String in Kotlin?

Bình luận

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

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