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

एक Kotlin class अपनी properties अपने constructor header में ही declare कर सकती है, methods class के अंदर fun होते हैं, और named parameters callers को किसी भी order में नाम से arguments pass करने देते हैं।

11 min read · 9 cards · 2 checks

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


Theory

Wह Class जो एक Line में खुद को Declare करती है

BCA403 के Java में, एक Event class define करने का मतलब था fields declare करना, एक constructor लिखना, और हर field assign करना: एक simple data holder के लिए एक dozen lines की ceremony।

Kotlin इसे collapse करता है। आप एक class की PROPERTIES इसके constructor header में ही declare कर सकते हैं:

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

एक line, और आपके पास दो properties, एक constructor, और read-only/mutable control वाली एक class है। यह lesson Kotlin classes, इनके concise constructors, properties और methods, और named parameters cover करता है, FestConnect Mobile की Event class को Java के boilerplate के एक fraction के साथ building करते हुए। आपके OOP concepts transfer होते हैं; syntax कहीं ज़्यादा leaner है।

Theory

Classes, Concise तरीके से

इसके header में एक primary constructor वाली एक Kotlin class properties को एक साथ declare और initialize करती है:

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

  • val name: एक read-only property
  • var seats: एक mutable property

वह single header Java के field-declarations-plus-constructor-assignment को replace करता है। एक object new के WITHOUT create कीजिए (Kotlin के पास कोई new keyword नहीं है):

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

Dot से properties access कीजिए: e.name, e.seats। Simple assignment से आगे की setup logic के लिए, एक init { } block construction के दौरान चलता है। Methods बस class के अंदर declared functions (fun) हैं। Concise, पर वही objects-with-data-and-behaviour जो आप जानते हैं।

Practical

एक Method और Named Parameters वाली Event Class

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 आपको एक function या constructor को arguments NAME से pass करने देता है, किसी भी order में:

Event(name = "Garba", seats = 350) या Event(seats = 350, name = "Garba")

दोनों काम करते हैं, क्योंकि हर argument labelled है। यह READABILITY improve करता है, especially कई parameters के साथ: createUser("Riya", 18, true, false) (booleans का मतलब क्या है?) की बजाय, createUser(name = "Riya", age = 18, isActive = true, isAdmin = false) call site पर खुद को document करता है।

आप इस exact idea से Dart (BCA405-02) में मिले, जहाँ named parameters Flutter की house style थे। Kotlin के पास भी हैं, और ये DEFAULT parameter values (अगला lesson) के साथ pair होकर functions को overloading के बिना flexible बनाते हैं। Named parameters थोड़ी verbosity के बदले बहुत clarity देते हैं।

Quiz

आप एक Kotlin class Event(val name: String, var seats: Int) से एक object कैसे create करते हैं?

  1. new Event("Garba", 350), Java की तरह new keyword इस्तेमाल करते हुए
  2. Event("Garba", 350): Kotlin के पास कोई 'new' keyword NAHIN है; आप directly constructor call करते हैं
  3. Event.create("Garba", 350)
  4. आप Kotlin में objects create नहीं कर सकते
Show the answer

Event("Garba", 350): Kotlin के पास कोई 'new' keyword NAHIN है; आप directly constructor call करते हैं

Kotlin के पास कोई 'new' keyword NAHIN है: आप directly constructor call करके एक object create करते हैं, Event("Garba", 350), जो एक new instance return करता है। यह Java (BCA403) से एक छोटा पर consistent difference है, जहाँ आप new Event(...) लिखते। Option A Java का new इस्तेमाल करता है, जो Kotlin में एक syntax error है। Option C एक factory method invent करता है जो standard तरीका नहीं है। Option D false है: Kotlin object-oriented है और objects readily create करता है, बस new के बिना। यह भी नोट कीजिए concise primary constructor class header में (val name, var seats) properties को एक line में declare और initialize करता है, Java की separate fields plus constructor assignments को replace करते हुए।

Think first

Kotlin ने कितनी Lines बचाईं Count कीजिए

Java में, एक name और seats वाली एक Event class को field declarations, एक constructor, और (अक्सर) getters/setters चाहिए। Kotlin इसे एक header line में करता है। Kotlin ने क्या eliminate किया, और यह क्यों matter करता है? फिर tap कीजिए।

Show the answer

Kotlin का class Event(val name: String, var seats: Int) eliminate करता है: (1) separate FIELD declarations (private String name; private int seats;), (2) parameter list और this.name = name assignments वाला CONSTRUCTOR, और (3) अक्सर GETTERS और SETTERS, क्योंकि Kotlin properties implicit accessors के साथ आती हैं (अगला lesson)। Java का जो 10-plus lines का boilerplate था वह ONE line बन जाता है। यह क्यों matter करता है: कम boilerplate मतलब लिखने, पढ़ने, और ग़लत करने के लिए कम code, और INTENT (इन properties वाली एक class) तुरंत clear है, ceremony में buried नहीं। यह conciseness Android के लिए Kotlin preferred होने का एक core reason है: same app में Java की तुलना में dramatically कम repetitive code है। OOP CONCEPTS identical हैं (class, constructor, properties, methods); Kotlin बस इन्हें कहीं कम ceremony के साथ express करता है। कम lines, कम bugs, clearer intent।

Watch out

Kotlin Class Traps

'new' इस्तेमाल करना: Kotlin के पास new नहीं है; directly constructor call कीजिए (Event(...))।

Header में val/var भूलना: class Event(name: String) name को एक constructor PARAMETER बनाता है, एक stored property नहीं; इसे एक property बनाने के लिए val/var add कीजिए।

Named Parameters को Defaults से Confuse करना: named parameters (नाम से) और default values related पर distinct हैं (defaults अगला lesson)।

बिना fun के Methods: methods class के अंदर fun से declared होते हैं।

Java-Style Getters की Expectation: Kotlin properties में implicit accessors हैं; e.getSeats() नहीं, e.seats इस्तेमाल कीजिए।

Theory

Classes हो गईं; अब Inheritance, एक Twist के साथ

FestConnect Mobile के पास concise Kotlin classes हैं। अगला lesson INHERITANCE और इसका Kotlin-specific rule cover करता है जो हर Java programmer को trip करता है: classes default से FINAL होती हैं, inheritance allow करने के लिए आपको explicitly एक class को open mark करना पड़ता है (Java का opposite)। Plus abstract classes, interfaces, property getters/setters, और visibility modifiers। Kotlin का OOP, इसके safety-first defaults के साथ, Android unit से पहले language complete करता है।

Summary

Key takeaways

  • एक Kotlin class properties को अपने primary constructor header में declare कर सकती है: class Event(val name: String, var seats: Int)।
  • Header में val = read-only property; var = mutable property; यह Java के field-plus-constructor boilerplate को replace करता है।
  • 'new' के बिना objects create कीजिए: val e = Event("Garba", 350); dot से properties access कीजिए (e.name)।
  • Methods class के अंदर functions (fun) हैं; init { } construction पर setup चलाता है।
  • Named parameters किसी भी order में नाम से arguments pass करते हैं: Event(name = "Garba", seats = 350), readability improve करते हुए।
  • Kotlin properties में implicit getters/setters हैं; e.getSeats() नहीं, e.seats इस्तेमाल कीजिए।
  • Memory hook: header में properties, कोई new नहीं, methods के लिए fun, नाम से arguments।

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

Object oriented concepts: properties, methods and basics of objects and classes in Kotlin; named parameters, constructors · Advance Mobile Technology - I (Major-11-02) · Gri-Learn