Abstraction using interfaces in kotlin
package com.example.my_kotlin_practice
fun main(args: Array<String>) {
val array = arrayOf(Rectangles2(),Circles2(),Squares2(),Picture())
for(shape in array) {
shape.name()
shape.xyz()
}
}
//In kotlin, we can use multiple inheritance functionality with the help of interface
abstract class Shapes2: Name,Xyz {
abstract val i:Int
abstract fun draw()
fun type() {}
}
interface Name {
fun name() //By default, functions inside interface are "abstract"
// fun xyz() {}
}
interface Xyz {
fun xyz()
}
class Rectangles2:Shapes2() {
override val i = 10
override fun draw() {
println("Drawing a Rectangle")
}
override fun name() {
println("Name is Rectangle")
}
override fun xyz() {
println("Xyz is inside Rectangles2")
}
}
class Circles2:Shapes2() {
override val i = 20
override fun draw() {
println("Drawing a Circle")
}
override fun name() {
println("Name is Circle")
}
override fun xyz() {
println("Xyz is inside Circles2")
}
}
class Squares2:Shapes2() {
override val i = 40
override fun draw() {
println("Drawing a Square")
}
override fun name() {
println("Name is Square")
}
override fun xyz() {
println("Xyz is inside Squares2")
}
}
class Picture: Name, Xyz {
override fun name() {
println("Name is Picture")
}
override fun xyz() {
println("Xyz is inside Picture")
}
}
OUTPUT :
Name is Rectangle Xyz is inside Rectangles2 Name is Circle Xyz is inside Circles2 Name is Square Xyz is inside Squares2 Name is Picture Xyz is inside Picture Process finished with exit code 0
Comments
Post a Comment