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

override Keyword क्या है

override keyword वह तरीका है जिससे आप Kotlin को बताते हैं 'हां, मैं जान-बूझकर इस inherited behavior को अपने version से बदल रहा हूं'।
Syntax
kotlin
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

markup
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()}")
}

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

markup
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")
}

Overriding Properties

Properties को bilkul functions की तरह override किया जा सकता है, override val/override var का उपयोग करते हुए, जब तक base property open declared हो।

उदाहरण: Overriding Properties

markup
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}")
}

Preventing Further Overrides with final

किसी override member से पहले final जोड़ना किसी भी आगे की subclass को इसे फिर से override करने से रोकता है, उस particular behavior को hierarchy के इस level पर lock करते हुए।

उदाहरण: Preventing Further Overrides with final

markup
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())
}
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. किसी inherited open member को replace करते समय override भूल जाना; Kotlin को Java के optional @Override के विपरीत इसे explicitly चाहिए।
  2. किसी ऐसे member पर override उपयोग करना जो base class में open declared नहीं था, जिसे compiler reject कर देता है।
  3. super.method() call करना भूल जाना जब overriding method को base behavior को पूरी तरह replace करने के बजाय extend करना चाहिए।
चैप्टर सारांश
  • Kotlin को override keyword चाहिए जब भी कोई 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:

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.