Deep Kotlin tutorial covering classes, primary constructor, secondary constructor, properties with examples, diagram, mistakes, and interview answers.

Classes, Constructors, and Properties

What is Classes, Constructors, and Properties?

Classes, Constructors, and Properties is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: classes, primary constructor, secondary constructor, properties, init block. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Classes, Constructors, and Properties in simple language
  • Use classes in real Kotlin or Android code
  • Explain Classes, Constructors, and Properties in interviews with examples and trade-offs

How it works

Classes, Constructors, and Properties: classes, primary constructor, secondary constructor, properties, init block works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

  1. Identify what problem Classes, Constructors, and Properties: classes, primary constructor, secondary constructor, properties, init block 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

  • classes: core concept in this lesson
  • primary constructor: core concept in this lesson
  • secondary constructor: core concept in this lesson
  • properties: core concept in this lesson
  • init block: core concept in this lesson
  • Class: blueprint that owns state and behavior
  • Interface: a contract with no state of its own
  • Inheritance: reusing behavior through an is-a relationship

Important differences

Concept 1Concept 2How to remember it
InheritanceCompositionInheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
InterfaceAbstract classAn interface is a pure contract with no state; an abstract class can hold shared state and partial implementation.
Data classSealed classA data class models one shape of data; a sealed class models a closed set of alternative shapes, useful for representing UI state.

Diagram

flowchart TD
A["1.5 Object-Oriented vs Functional Kotlin"] --> B["Classes, Constructors, and Properties"]
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

Sealed class for UI state

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}

Delegation instead of inheritance

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source

When to use it

Use it when: Use this to model real domain concepts clearly - a data class for a User or Order, a sealed class for a finite set of UI states, an interface for a contract that several classes share.

Avoid it when: Avoid deep inheritance hierarchies for code reuse alone; prefer composition when the relationship is not a genuine is-a relationship.

Common mistakes

  • Reaching for inheritance when composition would be more flexible
  • Building deep inheritance hierarchies that are hard to change safely
  • Putting mutable shared state directly on an open base class
  • Using a plain class where a sealed class would make invalid states unrepresentable
  • Skipping interfaces and coding directly against a concrete implementation, which hurts testability

Interview questions

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

1. What is Classes, Constructors, and Properties in 1.5 Object-Oriented vs Functional Kotlin?

Answer: Classes, Constructors, and Properties is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: classes, primary constructor, secondary constructor, properties, init block. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

2. How does Classes, Constructors, and Properties work in Kotlin or Android code?

Answer: Classes, Constructors, and Properties: classes, primary constructor, secondary constructor, properties, init block works by shaping responsibilities. Kotlin lets you combine OOP modeling with functional transformations: use objects for behavior and boundaries, then use pure functions for predictable data changes.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

3. What is one common mistake with Classes, Constructors, and Properties?

Answer: A common mistake is: Reaching for inheritance when composition would be more flexible Another is: Building deep inheritance hierarchies that are hard to change safely

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

4. How would you explain Classes, Constructors, and Properties with a small example?

Answer: Classes, Constructors, and Properties is an important Kotlin topic from 1.5 Object-Oriented vs Functional Kotlin. In this lesson we focus only on the concepts that belong together: classes, primary constructor, secondary constructor, properties, init block. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

sealed interface UiState {
object Loading : UiState
data class Content(val items: List<String>) : UiState
data class Error(val message: String) : UiState
}
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Building deep inheritance hierarchies that are hard to change safely

5. What is the trade-off of using Classes, Constructors, and Properties?

Answer: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.

interface Repository { fun load(): List<String> }
class CachingRepository(private val source: Repository) : Repository by source
  • Trade-off: Inheritance and Composition solve related but different problems: Inheritance reuses behavior through an is-a relationship and a fixed hierarchy; composition builds behavior from smaller has-a parts that can change independently.
  • Common mistake: Reaching for inheritance when composition would be more flexible

Practice task

Write one Kotlin example for Classes, Constructors, and Properties. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.