Inheritance with "super" and "override" keywords in kotlin

 package com.example.my_kotlin_practice


fun main(args: Array<String>)
{
val car = Car("Yellow")
println(car.name)
car.drive()
}
open class Vehicle(val color: String) {
init {
println("Vehicle class created")
}
open var name:String = "Vehicle"
open fun drive() {
println("Vehicle is driving")
}
}
class Car(val carColor: String):Vehicle(carColor) {
init {
println("Car class created")
}
override var name:String = "Car"
override fun drive() {
super.drive()
println("Car is driving")
}
}

OUTPUT :

Vehicle class created Car class created Car Vehicle is driving Car is driving Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin