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

Interface के Basics

एक interface एक वादा है जो कोई class करता है कि वह कुछ खास चीज़ें करेगा, जैसे exact तरीका बताए बिना abilities की एक checklist मान लेना।
Syntax
kotlin
interface InterfaceName {
    fun method(): ReturnType
    fun withDefault(): ReturnType = value
}

class ClassName : InterfaceName {
    override fun method(): ReturnType = value
}

Declaring an Interface

एक interface functions और properties का एक set define करता है जिन्हें कोई भी implementing class प्रदान करने के लिए सहमत होती है, यह specify किए बिना कि उन्हें कैसे implement होना चाहिए।

उदाहरण: Declaring an Interface

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

// Define a class `Person`
class Person(val name: String) : Greetable {
    override fun greet(): String = "Hello, I am $name"
}

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

Default Implementations in Interfaces

एक interface function सीधे एक body शामिल कर सकता है, एक default implementation प्रदान करते हुए जिसे classes जैसा है वैसा उपयोग कर सकती हैं या अपने version से override कर सकती हैं।

उदाहरण: Default Implementations in Interfaces

markup
// Define an interface `Greetable` describing required behavior
interface Greetable {
    // Define the function `greet` with no parameters and returning a String
    fun greet(): String = "Hello there!"
}

// Define a class `Robot`
class Robot : Greetable

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

Implementing an Interface

कोई class किसी interface को colon के बाद list करके implement करती है, फिर उसके किसी भी abstract members के लिए override प्रदान करती है जिनका कोई default implementation नहीं।

उदाहरण: Implementing an Interface

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(val title: String) : Playable {
    override fun play(): String = "Playing $title"
}

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

Properties in Interfaces

Interfaces abstract properties भी declare कर सकते हैं, जिनके लिए implementing classes को एक value या backing implementation प्रदान करनी चाहिए।

उदाहरण: Properties in Interfaces

markup
// Define an interface `HasArea` describing required behavior
interface HasArea {
    val area: Double
}

// Define a class `Square`
class Square(val side: Double) : HasArea {
    override val area: Double
        get() = 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}")
}
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. किसी interface में constructor-style state store करने की कोशिश करना; interfaces के पास constructors या backing-field state नहीं हो सकती, सिर्फ abstract या default-implemented members।
  2. ज़रूरी interface method implement करना भूल जाना, जिसे compiler implementing class में एक missing override के रूप में flag करता है।
  3. यह मान लेना कि बिना body वाला interface method अपने आप abstract-only है; interfaces सीधे default implementations भी प्रदान कर सकते हैं।
चैप्टर सारांश
  • interface functions और properties का एक contract declare करता है जिसे implementing classes को पूरा करना चाहिए।
  • Interface members default रूप से abstract हैं लेकिन interface में सीधे एक default implementation भी प्रदान कर सकते हैं।
  • कोई class class MyClass : MyInterface { ... } से किसी interface को implement करती है, किसी भी ज़रूरी members के लिए override प्रदान करते हुए।
  • classes से single-inheritance के विपरीत, कोई class कई interfaces implement कर सकती है।
🔒

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.