Deep Kotlin tutorial covering public, private, protected, internal with examples, diagram, mistakes, and interview answers.

Visibility Modifiers, open, final, and internal

What is Visibility Modifiers, open, final, and internal?

Visibility Modifiers, open, final, and internal is an important Kotlin topic from 1.8 Memory, Visibility, and Modifiers. In this lesson we focus only on the concepts that belong together: public, private, protected, internal, open, final. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Visibility Modifiers, open, final, and internal in simple language
  • Use public in real Kotlin or Android code
  • Explain Visibility Modifiers, open, final, and internal in interviews with examples and trade-offs

How it works

Visibility Modifiers, open, final, and internal: public, private, protected, internal, open, final works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.

  1. Identify what problem Visibility Modifiers, open, final, and internal: public, private, protected, internal, open, final solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • public: core concept in this lesson
  • private: core concept in this lesson
  • protected: core concept in this lesson
  • internal: core concept in this lesson
  • open: core concept in this lesson
  • final: core concept in this lesson
  • object: a class with exactly one instance
  • companion object: members callable through the class name

Important differences

Concept 1Concept 2How to remember it
classobjectclass can have many instances; object always has exactly one.
privateinternalprivate is visible only in the same file or class; internal is visible anywhere in the same module.
Regular functioninline functionA regular function is a normal call; an inline function's body is inserted at the call site, avoiding lambda allocation.

Diagram

flowchart TD
A["1.8 Memory, Visibility, and Modifiers"] --> B["Visibility Modifiers, open, final, and internal"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

Companion object as a factory

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")

Destructuring a data class

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)

When to use it

Use it when: Reach for this when it makes your code clearer, safer, or easier for the next developer to follow.

Avoid it when: Skip it when a simpler, more familiar approach already solves the problem without extra ceremony.

Common mistakes

  • Using a companion object for everything instead of plain top-level functions
  • Marking a function inline without a lambda parameter to justify it
  • Making everything public by default instead of choosing the narrowest visibility that works
  • Overusing destructuring on data classes whose field order can change
  • Reaching for a custom DSL when a simple function call would be clearer

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Visibility Modifiers, open, final, and internal in 1.8 Memory, Visibility, and Modifiers?

Answer: Visibility Modifiers, open, final, and internal is an important Kotlin topic from 1.8 Memory, Visibility, and Modifiers. In this lesson we focus only on the concepts that belong together: public, private, protected, internal, open, final. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

2. How does Visibility Modifiers, open, final, and internal work in Kotlin or Android code?

Answer: Visibility Modifiers, open, final, and internal: public, private, protected, internal, open, final works by giving Kotlin extra control over instance creation, visibility, and compiled output. object and companion object model a single shared instance, visibility modifiers decide what other files or modules can see, and features such as inline, reified, and destructuring remove boilerplate while the compiler still generates ordinary, predictable bytecode underneath.

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Marking a function inline without a lambda parameter to justify it

3. What is one common mistake with Visibility Modifiers, open, final, and internal?

Answer: A common mistake is: Using a companion object for everything instead of plain top-level functions Another is: Marking a function inline without a lambda parameter to justify it

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

4. How would you explain Visibility Modifiers, open, final, and internal with a small example?

Answer: Visibility Modifiers, open, final, and internal is an important Kotlin topic from 1.8 Memory, Visibility, and Modifiers. In this lesson we focus only on the concepts that belong together: public, private, protected, internal, open, final. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

class ApiClient private constructor(val baseUrl: String) {
companion object {
fun create(baseUrl: String) = ApiClient(baseUrl)
}
}
val client = ApiClient.create("https://api.skillupclass.com")
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Marking a function inline without a lambda parameter to justify it

5. What is the trade-off of using Visibility Modifiers, open, final, and internal?

Answer: class and object solve related but different problems: class can have many instances; object always has exactly one.

data class Point(val x: Int, val y: Int)
val (x, y) = Point(4, 9)
  • Trade-off: class and object solve related but different problems: class can have many instances; object always has exactly one.
  • Common mistake: Using a companion object for everything instead of plain top-level functions

Practice task

Write one Kotlin example for Visibility Modifiers, open, final, and internal. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.