← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 1 of 7

Class के Basics

एक class एक blueprint है जो describe करता है कि किसी object में किस तरह की जानकारी और abilities होनी चाहिए, जैसे cookies बनाने के लिए एक cookie cutter।
Syntax
kotlin
class ClassName {
    fun method() { }
}

class ClassName(val property: Type, var other: Type)

val instance = ClassName(value1, value2)

Declaring a Simple Class

एक class संबंधित data और behavior को एक नाम के तहत group करता है, class keyword से defined। बिना किसी properties या methods वाली class को भी declare और instantiate किया जा सकता है।

उदाहरण: Declaring a Simple Class

markup
// Define a class `Greeter`
class Greeter {
    // Define the function `greet` with no parameters
    fun greet() {
        // Print "Hello from Greeter!" to the console, with a trailing newline
        println("Hello from Greeter!")
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `greeter`, set to `Greeter()`
    val greeter = Greeter()
    greeter.greet()
}

Properties in the Primary Constructor

किसी class के parentheses में सीधे val या var के साथ parameters declare करना अपने आप properties बना देता है, अलग field declarations और assignments लिखने की ज़रूरत से बचते हुए।

उदाहरण: Properties in the Primary Constructor

markup
// Define a class `Dog`
class Dog(val name: String, val breed: String)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `dog`, set to `Dog("Rex", "Labrador")`
    val dog = Dog("Rex", "Labrador")
    // Print "${dog.name} is a ${dog.breed}" to the console, with a trailing newline
    println("${dog.name} is a ${dog.breed}")
}

Creating Instances

किसी class का instance उसके नाम को किसी function की तरह call करके बनाया जाता है, उसके primary constructor के parameters से मेल खाते arguments पास करते हुए।

Note: Java के विपरीत, Kotlin में कोई new keyword की ज़रूरत नहीं।

उदाहरण: Creating Instances

markup
// Define a class `Point`
class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `p1`, set to `Point(1, 2)`
    val p1 = Point(1, 2)
    // Declare a read-only variable `p2`, set to `Point(3, 4)`
    val p2 = Point(3, 4)
    // Print "p1=(${p1.x}, ${p1.y}), p2=(${p2.x}, ${p2.y})" to the console, with a trailing newline
    println("p1=(${p1.x}, ${p1.y}), p2=(${p2.x}, ${p2.y})")
}

Member Functions

किसी class body के अंदर declared functions member functions (methods) हैं जो उस instance की अपनी properties पर operate करते हैं, implicitly this के ज़रिए accessible।

उदाहरण: Member Functions

markup
// Define a class `Circle`
class Circle(val radius: Double) {
    // Define the function `area` with no parameters and returning a Double
    fun area(): Double = Math.PI * radius * radius
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `circle`, set to `Circle(3.0)`
    val circle = Circle(3.0)
    // Print "Area: ${circle.area()}" to the console, with a trailing newline
    println("Area: ${circle.area()}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि class keyword को एक primary constructor के लिए parentheses () चाहिए भले ही इसमें parameters हों, या कोई न होने पर उन्हें पूरी तरह छोड़ देना।
  2. यह न समझना कि primary constructor में val/var के साथ declared properties अपने आप accessible class members बन जाती हैं।
  3. एक object बनाना लेकिन constructor call करते समय () भूल जाना, जैसे Dog() के बजाय Dog लिखना।
चैप्टर सारांश
  • एक class class keyword के बाद एक नाम और, optionally, एक primary constructor के साथ declare होती है।
  • constructor parentheses के अंदर val/var से declared properties अपने आप class members बन जाती हैं।
  • किसी class का एक object इसके constructor को किसी function की तरह call करके बनाया जाता है: ClassName(...)।
  • Kotlin classes default रूप से final हैं (subclass नहीं की जा सकतीं) जब तक open चिह्नित न हों।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.