Open Classes क्या हैं
In this page:
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
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())
}
Login to try C/C++/Java/PHP code in the editor
Subclassing an Open Class
open चिह्नित एक class को class Sub : Base(constructorArgs) से extend किया जा सकता है, जो subclass instance बनाने के हिस्से के रूप में base class के constructor को call करता है।
उदाहरण: Subclassing an Open Class
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}")
}
Login to try C/C++/Java/PHP code in the editor
Open Members
किसी class को open चिह्नित करना सिर्फ subclassing की अनुमति देता है; जिस हर individual function या property को overridable होना चाहिए उसे भी अलग से open चिह्नित होना चाहिए।
उदाहरण: Open Members
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()}")
}
Login to try C/C++/Java/PHP code in the editor
Why Closed by Default Helps
explicitly open चाहिए होना class author को यह सोचने पर मजबूर करता है कि किसी class के कौन से हिस्से extend करने के लिए सुरक्षित हैं, subclasses में अनजाने behavior changes से बचते हुए जिन्हें एक वाकई closed design पूरी तरह रोक देती।
उदाहरण: Why Closed by Default Helps
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}")
}
Login to try C/C++/Java/PHP code in the editor
- किसी regular class को subclass करने की कोशिश करना और एक compiler error पाना, यह भूलते हुए कि Kotlin classes
final(non-inheritable) हैं जब तकopenचिह्नित न हों। - किसी class को
openचिह्नित करना लेकिन यह भूल जाना कि इसके individual members को भीopenचाहिए इससे पहले कि कोई subclass उन्हें override कर सके। - यह मान लेना कि
openclasses स्वाभाविक रूप से कम सुरक्षित हैं; inheritance के बारे में explicit होना असल में सुरक्षा के लिए एक सोचा-समझा Kotlin design choice है।
- Kotlin classes default रूप से
finalहैं, यानी इन्हें subclass नहीं किया जा सकता जब तक explicitlyopenचिह्नित न हों। - 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: