← Back to Kotlin Course | Chapter 7: Inheritance & Interfaces | Lesson 1 of 7

Open Classes क्या हैं

एक open class वह है जिसने नई, ज़्यादा specific classes में extend होने की permission दी है; regular classes default रूप से इसे मना करती हैं।
Syntax
kotlin
open class BaseClass(val property: Type) {
    open fun method() { }
}

class SubClass(property: Type) : BaseClass(property)

Classes Are Final by Default

Java के विपरीत, एक Kotlin class को subclass नहीं किया जा सकता जब तक इसे explicitly open चिह्नित न किया जाए। यह जान-बूझकर रखा गया default अनजाने fragile inheritance hierarchies को रोकता है।

उदाहरण: Classes Are Final by Default

markup
open class Animal(val name: String) {
    fun describe() = "Animal: $name"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `animal`, set to `Animal("Generic")`
    val animal = Animal("Generic")
    // Print `animal.describe()` to the console, with a trailing newline
    println(animal.describe())
}

Subclassing an Open Class

open चिह्नित एक class को class Sub : Base(constructorArgs) से extend किया जा सकता है, जो subclass instance बनाने के हिस्से के रूप में base class के constructor को call करता है।

उदाहरण: Subclassing an Open Class

markup
open class Animal(val name: String)

// Define a class `Dog`
class Dog(name: String) : Animal(name)

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

Open Members

किसी class को open चिह्नित करना सिर्फ subclassing की अनुमति देता है; जिस हर individual function या property को overridable होना चाहिए उसे भी अलग से open चिह्नित होना चाहिए।

उदाहरण: Open Members

markup
open class Animal(val name: String) {
    open fun sound(): String = "..."
}

// Define a class `Dog`
class Dog(name: String) : Animal(name) {
    override fun sound(): String = "Woof"
}

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

Why Closed by Default Helps

explicitly open चाहिए होना class author को यह सोचने पर मजबूर करता है कि किसी class के कौन से हिस्से extend करने के लिए सुरक्षित हैं, subclasses में अनजाने behavior changes से बचते हुए जिन्हें एक वाकई closed design पूरी तरह रोक देती।

उदाहरण: Why Closed by Default Helps

markup
open class Counter {
    open var count: Int = 0
        protected set

    open fun increment() {
        count++
    }
}

// Define a class `StepCounter`
class StepCounter : Counter() {
    override fun increment() {
        count += 2
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `counter`, set to `StepCounter()`
    val counter = StepCounter()
    counter.increment()
    counter.increment()
    // Print "Count: ${counter.count}" to the console, with a trailing newline
    println("Count: ${counter.count}")
}
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. किसी regular class को subclass करने की कोशिश करना और एक compiler error पाना, यह भूलते हुए कि Kotlin classes final (non-inheritable) हैं जब तक open चिह्नित न हों।
  2. किसी class को open चिह्नित करना लेकिन यह भूल जाना कि इसके individual members को भी open चाहिए इससे पहले कि कोई subclass उन्हें override कर सके।
  3. यह मान लेना कि open classes स्वाभाविक रूप से कम सुरक्षित हैं; inheritance के बारे में explicit होना असल में सुरक्षा के लिए एक सोचा-समझा Kotlin design choice है।
चैप्टर सारांश
  • Kotlin classes default रूप से final हैं, यानी इन्हें subclass नहीं किया जा सकता जब तक explicitly open चिह्नित न हों।
  • Individual members (functions, properties) को भी open चाहिए इससे पहले कि कोई subclass उन्हें override कर सके।
  • यह 'closed by default' design उन languages में आम fragile base class समस्याओं से बचाता है जहां सब कुछ default रूप से inheritable है।
  • एक subclass class Sub : Base(...) से declare होता है, base class के constructor को call करते हुए।
🔒

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.