Kotlin – Abstract Classes

A class which is declared with abstract keyword is known as abstract class. An abstract class cannot be instantiated. Means, we cannot create object of abstract class. The method and properties of abstract class are non-abstract unless they are explicitly declared as abstract.

Declaration of abstract class :

abstract class A {
var x = 0
    abstract fun doSomething()
}

Abstract classes are partially defined classes, methods and properties which are no implementation but must be implemented into derived class. If the derived class does not implement the properties of base class then is also meant to be an abstract class.Abstract class or abstract function does not need to annotate with open keyword as they are open by default. Abstract member function does not contain its body. The member function cannot be declared as abstract if it contains in body in abstract class.

  • Example of abstract class that has abstract method : In this example, there is an abstract class Car that contains an abstract function run(). The implementation of run() function is provided by its subclass Honda.
abstract class Car{
    abstract fun run()
}
class Honda: Car(){
   override fun run(){
println("Honda is running safely..")
   }
}
fun main(args: Array<String>){
val obj = Honda()
obj.run();
}

Output :

Honda is running safely..

A non-abstract open member function can be over ridden in an abstract class.

open class Car {
    open fun run() {
        println("Car is running..")
    }
}
    abstract class Honda : Car() {
        override abstract fun run()
    }
    class City: Honda(){
        override fun run() {
          // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        println("Honda City is running..")
        }
    }
    fun main(args: Array<String>){
    val car = Car()
    car.run()
    val city = City()
    city.run()
}

Output :

Car is running..
Honda City is running..

In above example, An abstract class Honda extends the class Car and its function run(). Honda class override the run() function of Car class. The Honda class did not give the implementation of run() function as it is also declared as abstract. The implementation of abstract function run() of Honda class is provided by City class.

  • Example of real scenario of abstract class : In this example, an abstract class Bank that contains an abstract function simpleInterest() accepts three parameters p,r,and t. The class SBI and PNB provides the implementation of simpleInterest() function and returns the result.
abstract class Bank {
    abstract fun simpleInterest(p: Int, r: Double, t: Int): Double
}

class SBI : Bank() {
    override fun simpleInterest(p: Int, r: Double, t: Int): Double {
        return (p * r * t) / 100
    }
}

class PNB : Bank() {
    override fun simpleInterest(p: Int, r: Double, t: Int): Double {
        return (p * r * t) / 100
    }
}

fun main(args: Array<String>) {
    var sbi: Bank = SBI()
    val sbiint = sbi.simpleInterest(1000, 5.0, 3)
    println("SBI interest is $sbiint")
    var pnb: Bank = PNB()
    val pnbint = pnb.simpleInterest(1000, 4.5, 3)
    println("PNB interest is $pnbint")
}  

Output :

SBI interest is 150.0
PNB interest is 135.0

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.