Kotlin Abstract Class

A class which is declared with keyword ‘abstract’ is termed as Abstract Class, these can also be termed as classes which can’t be instantiated. Or in Simple words, we cannot create objects of Abstract classes. Similar to Java, abstract classes in Kotlin can be inherited in sub classes.

Kotlin Abstract Class

All the members of the abstract class including the properties and methods are non-abstract by default until we declare them as “abstract”. Consider the example below, where we have taken method greet as abstract method.

abstract class Area {
    var pi : Double = 3.17
    // Here area variable is abstracty which means, 
    // it has to be overridden in sub class.
    abstract var area: Double  
    // function display area is a non abstract method, and
    // needs not to be overridden.
    fun displayArea() {
        println("The area is $area.")
    }
    //abstract method calculateArea, it needs to be overridden.
    abstract fun calculateArea()
}

Based on the example above we can understand the below points:

  1. We have created an abstract class “Area” and this class cannot be instantiated or you cannot create objects of the class.
  2. Class area has abstract properties and methods:
    1. area is an abstract property and it needs to overridden in child or sub class.
    2. calculateArea() is an abstract method and it needs to be implemented in sub class.
  3. Class Area has non abstract properties and methods, which cannot be overridden or implemented:
      1. pi is a non-abstract property.
      2. displayArea() is a non-abstract property.
  4. If the abstract properties and methods are not overridden in the sub or child class, the subclass also needs to be declared as “abstract”.
  5. In case if we wish to override the non-abstract properties or methods, we need to declare class as “open”.
  6. Last but not the least, point to ponder is that abstract class are by default always open and hence don’t need to be declared the same.

Let’s have a look at detailed example which uses the concept of abstract classes and inheritance.

abstract class Area {
    var pi : Double = 3.17
    
    // Here area variable is abstracty which means, 
    // it has to be overridden in sub class.
    abstract var area: Double
    
    // function display area is a non abstract method, and
    // needs not to be overridden.
    fun displayArea() {
        println("The area is $area.")
    }
    //abstract method calculateArea, it needs to be overridden.
    abstract fun calculateArea()
}
class Square(side: Double): Area() {
    override var area: Double = 0.0
     val x = side
    override fun calculateArea(){
    	area = x * x
    }
}
fun main(args: Array<String>) {
    val sqObj = Square(10.00)
    sqObj.calculateArea()
    sqObj.displayArea()
}

Output:

The area is 100.0.

Based on the output we understood that the class Area was extended in Square class which implemented the method calculateArea() and override the variable area. Both these property and method though declared in Area class have behaviour different for each of the sub class. Let’s extend the above example and use 2 classes, Square and Circle and have a look.

Abstract Class with Inheritance

abstract class Area {
    var pi : Double = 3.17
    // Here area variable is abstracty which means, 
    // it has to be overridden in sub class.
    abstract var area: Double
    // function display area is a non abstract method, and
    // needs not to be overridden.
    fun displayArea() {
        println("The area is $area.")
    }
    //abstract method calculateArea, it needs to be overridden.
    abstract fun calculateArea()
}
class Square(side: Double): Area() {
    override var area: Double = 0.0
     val x = side
    override fun calculateArea(){
    	area = x * x
    }
}
class Circle(radius: Double): Area() {
    override var area: Double = 0.0
     val x = radius
    override fun calculateArea(){
    	area = pi * x * x
    }
}
fun main(args: Array<String>) {
    val sqObj = Square(10.00)
    sqObj.calculateArea()
    sqObj.displayArea() 
    val circleObj = Circle(10.00)
    circleObj.calculateArea()
    circleObj.displayArea()
}

Output:

The area is 100.0.
The area is 317.0.

As evident and visible both the functions used “calculateArea”, have exact same name but perform different operation or body based on the object which is being used to call. Using abstract class we can make our code modular and easy to understand. It also extends the functionality to use the same method and property name across all sub classes make code uniform.

Conclusion

Based on the tutorial it is evident that we can use these abstract classes when we want use functions (methods) and properties (variables) with different functionality common across the sub classes yet different. Key point to note is that abstract class can hold both abstract as well as non-abstract elements.

That was all for Kotlin  Abstract classes, and it make much sense to use Abstract classes with inheritance.

Leave a Comment

Your email address will not be published. Required fields are marked *