Kotlin Abstract Class: In this tutorial, you will learn about abstract class and how to implement it in Kotlin (with the assistance of examples).

A class that is declared with an abstract watchword is known as an abstract class. An abstract class can’t be instantiated. This means we can’t create an object of a unique class. The strategy and properties of an abstract class are non-abstract unless if they are expressly proclaimed as abstract.

Like Java, the abstract catchphrase is used to pronounce abstract classes in Kotlin. An abstract class can’t be launched (you can’t create objects of an abstract class). Nonetheless, you can acquire subclasses from can them.

The individuals (properties and techniques) of an abstract class are non-abstract unless you explicitly use abstract watchword to make them abstract. Let’s take an example:

abstract class Person {
    
    var age: Int = 40

    fun displaySSN(ssn: Int) {
        println("My SSN is $ssn.")
    }

    abstract fun displayJob(description: String)
}

Here,

  • an abstract class Person is created. You can’t create objects of the class.
  • the class has a non-abstract property age and a non-abstract strategy displaySSN(). In the event that you need to override these individuals in the subclass, they ought to be set apart with an open watchword.
  • The class has an abstract strategy displayJob(). It doesn’t have any execution and should be overriden in its subclasses.

Note: Abstract classes are consistently open. You don’t have to expressly use open watchword to acquire subclasses from them.


Example: Kotlin Abstract Class and Method

abstract class Person(name: String) {

    init {
        println("My name is $name.")
    }

    fun displaySSN(ssn: Int) {
        println("My SSN is $ssn.")
    }

    abstract fun displayJob(description: String)
}

class Teacher(name: String): Person(name) {

    override fun displayJob(description: String) {
        println(description)
    }
}

fun main(args: Array<String>) {
    val salman = Teacher("Salman Khan")
    salman.displayJob("I'm a computer teacher.")
    salman.displaySSN(23123)
}

At the point when you run the program, the output will be:

My name is Salman Khan.
I'm a computer teacher.
My SSN is 23123.

Here, a class Teacher is derived from an abstract class Person.

An object Salman of Teacher class is started up. We have passed “Salman Khan” as a parameter to the essential constructor while creating it. This executes the initializer block of the Person class.

At that point, displayJob() technique is called using Salman object. Note, that the displayJob() strategy is declared abstract in the base class, and overridden in the derived class.

At long last, displaySSN() technique is called using salman object. The strategy is non-abstract and declared in Person class (and not declared in Teacher class).


Recommended Reading: Kotlin Interfaces

Kotlin interfaces are like abstract classes. Notwithstanding, interfaces can’t store state while abstract classes can.

This means the interface may have property however it should be abstract or needs to provide accessor executions. Though, it’s not required for the property of an abstract class to be abstract.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply