Abstract Class in Kotlin : Why They are Useful?

Home » Kotlin » Abstract Class in Kotlin : Why They are Useful?

Abstract classes are an essential feature in the Kotlin language that enables developers to define a common interface for a set of related classes while leaving the implementation of some methods up to the concrete subclasses.In this tutorial, we will explore what abstract classes are in Kotlin, how to use them, and why they are useful. Let’s start with an abstract class in Kotlin.

Abstract Class in Kotlin : Why They are Useful?

What are abstract classes in Kotlin?

An abstract class in Kotlin is a class that cannot be instantiated directly but must be subclassed for use.

An abstract class can contain abstract methods, which have no implementation, and concrete methods, which have an implementation.

How to Use Abstract Classes in Kotlin

Using abstract classes in Kotlin involves defining and implementing abstract methods within those classes. Here’s a step-by-step guide:

Step 1: Declare an Abstract Class

abstract class School {
   
    abstract fun studentInfo()
    

}

In this example, School is an abstract class with an abstract method studentInfo() We can also include concrete methods within an abstract class, but it is essential to ensure that these concrete methods have a body. For example:

abstract class School {
   
    abstract fun studentInfo()
    fun schoolInfo() = println("LLM School Germany");

}

Step 2 : Create concrete Subclasses

Subclass the abstract class and provide concrete implementations for the abstract methods.

class Teacher() : School(){
    override fun studentInfo() {
        println("Showing Student Info...")
    }
}

Step 3: Let’s Create Objects

Create instances of the concrete subclass / subclasses and use their methods.

    val teacher = Teacher();
    teacher.studentInfo();
    teacher.schoolInfo();

By following these steps, you can effectively use abstract classes in Kotlin to define a common interface for a set of related classes while leaving the implementation details to the concrete subclasses.

Program Example:

abstract_class_example.kt

Open the Kotlin playground and Write the following code:

abstract class School {
   
    abstract fun studentInfo()
    fun schoolInfo() = println("LLM School Germany");

}
class Teacher() : School(){
    override fun studentInfo() {
        println("Showing Student Info...")
    }
}

fun main() 
{
    val teacher = Teacher();
    teacher.studentInfo();
    teacher.schoolInfo();
}

Output:

Showing Student Info...
LLM School Germany

How can one determine whether a method is concrete or abstract in Kotlin?

In Kotlin, we can use reflection to determine the method.

Program Example:

import kotlin.reflect.full.*;

abstract class MyAbsClass
{
    abstract fun demo()
    abstract fun message()
    fun show() = println("Hello")
    fun foo() = 8
   
}
fun main() {

    val check = MyAbsClass::class
    check.declaredMemberFunctions.forEach{
        function -> 
            if(function.isAbstract){
          println("${function.name} is an abstract method.")
          }else {
            println("${function.name} is a concreate method")
          }
            
        }
    }

Output:

demo is an abstract method.
foo is a concreate method
message is an abstract method.
show is a concreate method

Abstract Class in Kotlin : Pros

Abstract classes can have both abstract and concrete methods, allowing a mix of shared and specific functionality.

Helps in organizing and structuring code by grouping related methods and properties in a common base class.

Abstract classes allow you to define common methods and properties that can be inherited by multiple subclasses.

Abstract Methods

Allows you to define the signature of methods without specifying the implementation details, offering flexibility to subclasses.

Conclusion

Using abstract classes and methods in your Kotlin code can contribute to better code organization, reuse, and maintenance.

Happy Coding!

You may also like...