override Keyword क्या है
In this page:
open class BaseClass {
open fun method() { }
open val property: Type = value
}
class SubClass : BaseClass() {
override fun method() { super.method() }
override val property: Type = otherValue
}
Overriding a Function
किसी subclass में override fun उपयोग करना base class के open implementation को एक नए से बदल देता है, और Kotlin में यह keyword अनिवार्य है, overrides को explicit और पहचानने में आसान बनाते हुए।
उदाहरण: Overriding a Function
open class Shape {
open fun area(): Double = 0.0
}
// Define a class `Square`
class Square(val side: Double) : Shape() {
override fun area(): Double = side * side
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `square`, set to `Square(4.0)`
val square = Square(4.0)
// Print "Area: ${square.area()}" to the console, with a trailing newline
println("Area: ${square.area()}")
}
Login to try C/C++/Java/PHP code in the editor
Calling the Base Implementation with super
किसी override के अंदर super.member base class का original version call करता है, किसी subclass को inherited behavior को पूरी तरह replace करने के बजाय extend करने देते हुए।
उदाहरण: Calling the Base Implementation with super
open class Logger {
open fun log(message: String) {
// Print "[LOG] $message" to the console, with a trailing newline
println("[LOG] $message")
}
}
// Define a class `TimestampedLogger`
class TimestampedLogger : Logger() {
override fun log(message: String) {
super.log("2024: $message")
}
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `logger` (a `Logger`), set to `TimestampedLogger()`
val logger: Logger = TimestampedLogger()
logger.log("Application started")
}
Login to try C/C++/Java/PHP code in the editor
Overriding Properties
Properties को bilkul functions की तरह override किया जा सकता है, override val/override var का उपयोग करते हुए, जब तक base property open declared हो।
उदाहरण: Overriding Properties
open class Vehicle {
open val maxSpeed: Int = 100
}
// Define a class `SportsCar`
class SportsCar : Vehicle() {
override val maxSpeed: Int = 300
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `car` (a `Vehicle`), set to `SportsCar()`
val car: Vehicle = SportsCar()
// Print "Max speed: ${car.maxSpeed}" to the console, with a trailing newline
println("Max speed: ${car.maxSpeed}")
}
Login to try C/C++/Java/PHP code in the editor
Preventing Further Overrides with final
किसी override member से पहले final जोड़ना किसी भी आगे की subclass को इसे फिर से override करने से रोकता है, उस particular behavior को hierarchy के इस level पर lock करते हुए।
उदाहरण: Preventing Further Overrides with final
open class Base {
open fun greet() = "Hello from Base"
}
open class Middle : Base() {
final override fun greet() = "Hello from Middle"
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `middle` (a `Base`), set to `Middle()`
val middle: Base = Middle()
// Print `middle.greet()` to the console, with a trailing newline
println(middle.greet())
}
Login to try C/C++/Java/PHP code in the editor
- किसी inherited open member को replace करते समय
overrideभूल जाना; Kotlin को Java के optional@Overrideके विपरीत इसे explicitly चाहिए। - किसी ऐसे member पर
overrideउपयोग करना जो base class मेंopendeclared नहीं था, जिसे compiler reject कर देता है। super.method()call करना भूल जाना जब overriding method को base behavior को पूरी तरह replace करने के बजाय extend करना चाहिए।
- Kotlin को
overridekeyword चाहिए जब भी कोई subclass किसी inherited open member को replace करे। - override किए जा रहे base member को खुद
openचिह्नित होना चाहिए (या एक abstract/interface member होना चाहिए)। super.memberकिसी override के अंदर से base class का original implementation call करता है।- एक overriding member खुद किसी गहरी subclass में आगे override होने के लिए open है, जब तक
finalचिह्नित न हो।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: