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

कई Interfaces

Multiple interfaces एक class को एक साथ कई अलग checklists of abilities मानने देते हैं, कुछ जो एक single base class नहीं कर सकती।
Syntax
kotlin
class ClassName : BaseClass(), InterfaceA, InterfaceB {
    override fun method() {
        super<InterfaceA>.method()   // pick which default to use
    }
}

Implementing Several Interfaces

कोई class colon के बाद commas से अलग करके वे सभी interfaces list करती है जिन्हें यह implement करती है, और हर एक से हर abstract member को satisfy करना चाहिए।

उदाहरण: Implementing Several Interfaces

markup
// Define an interface `Flyable` describing required behavior
interface Flyable {
    // Define the function `fly` with no parameters and returning a String
    fun fly(): String
}

// Define an interface `Swimmable` describing required behavior
interface Swimmable {
    // Define the function `swim` with no parameters and returning a String
    fun swim(): String
}

// Define a class `Duck`
class Duck : Flyable, Swimmable {
    override fun fly(): String = "Duck is flying"
    override fun swim(): String = "Duck is swimming"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `duck`, set to `Duck()`
    val duck = Duck()
    // Print `duck.fly()` to the console, with a trailing newline
    println(duck.fly())
    // Print `duck.swim()` to the console, with a trailing newline
    println(duck.swim())
}

Resolving Conflicting Default Methods

अगर दोनों interfaces एक ही नाम वाले method के लिए एक default implementation प्रदान करते हैं, तो implementing class को ambiguity हल करने के लिए इसे खुद override करना चाहिए।

उदाहरण: Resolving Conflicting Default Methods

markup
// Define an interface `A` describing required behavior
interface A {
    // Define the function `describe` with no parameters and returning a String
    fun describe(): String = "From A"
}

// Define an interface `B` describing required behavior
interface B {
    // Define the function `describe` with no parameters and returning a String
    fun describe(): String = "From B"
}

// Define a class `C`
class C : A, B {
    override fun describe(): String = super<A>.describe() + " and " + super<B>.describe()
}

// Entry point: execution of the program starts here
fun main() {
    // Print `C().describe()` to the console, with a trailing newline
    println(C().describe())
}

Combining a Base Class and Interfaces

जब कोई class एक base class extend भी करती है और interfaces implement भी करती है, base class list में पहले आती है, उसके बाद interfaces, सब commas से अलग।

उदाहरण: Combining a Base Class and Interfaces

markup
open class Animal(val name: String)

// Define an interface `Trainable` describing required behavior
interface Trainable {
    // Define the function `train` with no parameters and returning a String
    fun train(): String
}

// Define a class `Dog`
class Dog(name: String) : Animal(name), Trainable {
    override fun train(): String = "$name learned to sit"
}

// 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.train()` to the console, with a trailing newline
    println(dog.train())
}

Interfaces as Type Contracts

किसी variable को एक interface type के साथ declare किया जा सकता है भले ही यह किसी concrete class का instance रखता हो, code को सिर्फ एक specific implementation के बजाय shared contract पर निर्भर रहने देते हुए।

उदाहरण: Interfaces as Type Contracts

markup
// Define an interface `Playable` describing required behavior
interface Playable {
    // Define the function `play` with no parameters and returning a String
    fun play(): String
}

// Define a class `Song`
class Song : Playable {
    override fun play(): String = "Playing a song"
}

// Define a class `Video`
class Video : Playable {
    override fun play(): String = "Playing a video"
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `items` (a `List<Playable>`), set to `listOf(Song(), Video())`
    val items: List<Playable> = listOf(Song(), Video())
    // Loop over `items`, binding each item to `item`
    for (item in items) {
        // Print `item.play()` to the console, with a trailing newline
        println(item.play())
    }
}
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. यह मान लेना कि multiple interface implementation multiple class inheritance जैसा ही है; Kotlin अब भी सिर्फ एक base class की अनुमति देता है लेकिन कई interfaces की।
  2. जब दो interfaces एक ही default method name प्रदान करें तब disambiguate करना भूल जाना, जिसके लिए एक explicit super<InterfaceName>.method() call चाहिए।
  3. किसी base class के बाद interfaces को गलत जगह list करना, यह न समझते हुए कि वहां interfaces के लिए order मायने नहीं रखता (हालांकि class को मौजूद होने पर पहले आना चाहिए)।
चैप्टर सारांश
  • कोई class colon के बाद commas से अलग किए किसी भी संख्या में interfaces implement कर सकती है।
  • जब दो implemented interfaces एक ही default method प्रदान करें, class को इसे override करना चाहिए और super<InterfaceName>.method() से एक specific को call कर सकती है।
  • एक single base class (अगर कोई हो) पहले listed होती है, उसके बाद interfaces, सब commas से अलग।
  • कई interfaces implement करना वह तरीका है जिससे Kotlin behavior contracts के लिए multiple inheritance का एक रूप achieve करता है।
🔒

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.