Object oriented concepts: properties, methods and basics of objects and classes in Kotlin; named parameters, constructors

A Kotlin class can declare its properties right in its constructor header, methods are fun inside the class, and named parameters let callers pass arguments by name in any order.

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The class that declares itself in one line

In BCA403's Java, defining an Event class meant declaring fields, writing a constructor, and assigning each field: a dozen lines of ceremony for a simple data holder.

Kotlin collapses this. You can declare a class's PROPERTIES right in its constructor header:

class Event(val name: String, var seats: Int)

One line, and you have a class with two properties, a constructor, and read-only/mutable control. This lesson covers Kotlin classes, their concise constructors, properties and methods, and named parameters, building FestConnect Mobile's Event class with a fraction of Java's boilerplate. Your OOP concepts transfer; the syntax is far leaner.

Theory

Classes, the concise way

A Kotlin class with a primary constructor in its header declares and initializes properties at once:

class Event(val name: String, var seats: Int)

  • val name: a read-only property
  • var seats: a mutable property

That single header replaces Java's field-declarations-plus-constructor-assignment. Create an object WITHOUT new (Kotlin has no new keyword):

val e = Event("Garba Night", 350)

Access properties with the dot: e.name, e.seats. For setup logic beyond simple assignment, an init { } block runs during construction. Methods are just functions (fun) declared inside the class. Concise, but the same objects-with-data-and-behaviour you know.

Practical

An Event class with a method and named parameters

class Event(val name: String, var seats: Int) {
    // A METHOD (fun inside the class)
    fun book(): Boolean {
        return if (seats > 0) { seats--; true } else false
    }

    fun describe() = "$name: $seats seats"   // one-expression function
}

fun main() {
    // Create WITHOUT 'new':
    val e = Event("Garba Night", 350)
    println(e.describe())        // Garba Night: 350 seats
    e.book()                     // seats now 349
    println(e.seats)             // 349

    // NAMED PARAMETERS: pass by name, any order
    val e2 = Event(seats = 60, name = "Coding Contest")
    println(e2.describe())       // Coding Contest: 60 seats
}

Theory

Named parameters

Kotlin lets you pass arguments to a function or constructor by NAME, in any order:

Event(name = "Garba", seats = 350) or Event(seats = 350, name = "Garba")

Both work, because each argument is labelled. This improves READABILITY, especially with many parameters: instead of createUser("Riya", 18, true, false) (what do the booleans mean?), createUser(name = "Riya", age = 18, isActive = true, isAdmin = false) documents itself at the call site.

You met this exact idea in Dart (BCA405-02), where named parameters were Flutter's house style. Kotlin has them too, and they pair with DEFAULT parameter values (next lesson) to make functions flexible without overloading. Named parameters trade a little verbosity for a lot of clarity.

Quiz

How do you create an object from a Kotlin class Event(val name: String, var seats: Int)?

  1. new Event("Garba", 350), using the new keyword like Java
  2. Event("Garba", 350): Kotlin has NO 'new' keyword; you call the constructor directly
  3. Event.create("Garba", 350)
  4. You cannot create objects in Kotlin
Show the answer

Event("Garba", 350): Kotlin has NO 'new' keyword; you call the constructor directly

Kotlin has NO 'new' keyword: you create an object by calling the constructor directly, Event("Garba", 350), which returns a new instance. This is a small but consistent difference from Java (BCA403), where you would write new Event(...). Option A uses Java's new, which is a syntax error in Kotlin. Option C invents a factory method that is not the standard way. Option D is false: Kotlin is object-oriented and creates objects readily, just without new. Also note the concise primary constructor in the class header (val name, var seats) declares and initializes the properties in one line, replacing Java's separate fields plus constructor assignments.

Think first

Count the lines Kotlin saved

In Java, an Event class with a name and seats needs field declarations, a constructor, and (often) getters/setters. Kotlin does it in one header line. What did Kotlin eliminate, and why does that matter? Then tap.

Show the answer

Kotlin's class Event(val name: String, var seats: Int) eliminates: (1) the separate FIELD declarations (private String name; private int seats;), (2) the CONSTRUCTOR with its parameter list and this.name = name assignments, and (3) often the GETTERS and SETTERS, because Kotlin properties come with implicit accessors (next lesson). What was 10-plus lines of Java boilerplate becomes ONE line. Why it matters: less boilerplate means less code to write, read, and get wrong, and the INTENT (a class with these properties) is immediately clear, not buried in ceremony. This conciseness is a core reason Kotlin is preferred for Android: the same app has dramatically less repetitive code than in Java. The OOP CONCEPTS are identical (class, constructor, properties, methods); Kotlin just expresses them with far less ceremony. Fewer lines, fewer bugs, clearer intent.

Watch out

Kotlin class traps

Using 'new': Kotlin has no new; call the constructor directly (Event(...)).

Forgetting val/var in the header: class Event(name: String) makes name a constructor PARAMETER, not a stored property; add val/var to make it a property.

Confusing named parameters with defaults: named parameters (by name) and default values are related but distinct (defaults next lesson).

Methods without fun: methods are declared with fun inside the class.

Expecting Java-style getters: Kotlin properties have implicit accessors; use e.seats, not e.getSeats().

Theory

Classes down; now inheritance, with a twist

FestConnect Mobile has concise Kotlin classes. The next lesson covers INHERITANCE and its Kotlin-specific rule that trips up every Java programmer: classes are FINAL by default, you must explicitly mark a class open to allow inheritance (the opposite of Java). Plus abstract classes, interfaces, property getters/setters, and visibility modifiers. Kotlin's OOP, with its safety-first defaults, completes the language before the Android unit.

Summary

Key takeaways

  • A Kotlin class can declare properties in its primary constructor header: class Event(val name: String, var seats: Int).
  • val in the header = read-only property; var = mutable property; this replaces Java's field-plus-constructor boilerplate.
  • Create objects WITHOUT 'new': val e = Event("Garba", 350); access properties with the dot (e.name).
  • Methods are functions (fun) inside the class; init { } runs setup at construction.
  • Named parameters pass arguments by name in any order: Event(name = "Garba", seats = 350), improving readability.
  • Kotlin properties have implicit getters/setters; use e.seats, not e.getSeats().
  • Memory hook: properties in the header, no new, fun for methods, arguments by name.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from OOPS Concepts with Kotlin

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati