Interface के Basics
In this page:
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Default Implementations in Interfaces
एक interface function सीधे एक body शामिल कर सकता है, एक default implementation प्रदान करते हुए जिसे classes जैसा है वैसा उपयोग कर सकती हैं या अपने version से override कर सकती हैं।
उदाहरण: Default Implementations in Interfaces
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Implementing an Interface
कोई class किसी interface को colon के बाद list करके implement करती है, फिर उसके किसी भी abstract members के लिए override प्रदान करती है जिनका कोई default implementation नहीं।
उदाहरण: Implementing an Interface
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Properties in Interfaces
Interfaces abstract properties भी declare कर सकते हैं, जिनके लिए implementing classes को एक value या backing implementation प्रदान करनी चाहिए।
उदाहरण: Properties in Interfaces
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
- किसी interface में constructor-style state store करने की कोशिश करना; interfaces के पास constructors या backing-field state नहीं हो सकती, सिर्फ abstract या default-implemented members।
- ज़रूरी interface method implement करना भूल जाना, जिसे compiler implementing class में एक missing override के रूप में flag करता है।
- यह मान लेना कि बिना body वाला interface method अपने आप abstract-only है; interfaces सीधे default implementations भी प्रदान कर सकते हैं।
interfacefunctions और 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: