Abstraction in kotlin (abstract class and abstract methods in kotlin)

 package com.example.my_kotlin_practice


fun main(args: Array<String>)
{
val array = arrayOf(Rectangles(),Circles(),Squares())
for(shape in array) {
shape.draw()
shape.name()
}
}

abstract class Shapes {
abstract val i: Int
abstract fun draw()
abstract fun name()
fun type() {
println("This is non-abstract method")
}
}
class Rectangles:Shapes() {
override val i:Int = 10
override fun draw(){
println("Drawing a Rectangle")
}
override fun name() {
println("Name is Rectangle")
}

}
class Circles:Shapes() {
override val i:Int = 20
override fun draw() {
println("Drawing a Circle")
}
override fun name() {
println("Name is Circle")
}
}
class Squares:Shapes() {
override val i:Int = 40
override fun draw() {
println("Drawing a Square")
}
override fun name() {
println("Name is Square")
}
}

OUTPUT :

Drawing a Rectangle Name is Rectangle Drawing a Circle Name is Circle Drawing a Square Name is Square Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin