Table of contents
1.
Introduction
2.
Abstract Class
2.1.
Abstract Class Declaration 
2.2.
Examples
3.
Multiple derived Classes
4.
FAQs
5.
Key Takeaways  
Last Updated: Mar 27, 2024

Kotlin Abstract Classes

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into Kotlin Abstract Class. We will see how an abstract class is defined and how its characteristics can be overridden in its derived classes. We can not make an object of an abstract class, but an object can be formed out of a non-abstract class derived from this class.

If you are new to Kotlin and don’t know how functions are instantiated and used in Kotlin, you can check out our other article on Kotlin Functions

Abstract Class

An abstract class is used to state some basic characteristics. In Kotlin, an abstract class can not be instantiated. We can not make an object of an abstract class; trying to do the same will lead to a compilation error.

Abstract Class Declaration 

To declare an abstract class in Kotlin, we use the abstract keyword.

abstract class class_name{
    //body of the class
}

Examples

In this example, we will see how an abstract class can be declared and how a derived class can be made out of an abstract class. 

// Creating an abstract class
abstract class GradeSheet(){
    // abstract variables
    abstract var maths:Int
    abstract var english:Int
    abstract var science:Int

    // abstract method
    abstract fun total_marks()
}

// derived class
class Marks(_maths: Int, _english : Int, _science :Int): GradeSheet(){
    // overriding the values of abstract variables
    override var maths=_maths
    override var english=_english
    override var science=_science

    // overriding the total_marks function to calculate the total
    override fun total_marks(){
        var total=maths+english+science;
        println("Total Marks are: " + total)
    }
}

fun main(){
    // Making an object of marks class
    val ram_marks = Marks(50,89,76)
    // Printing the total_marks associated with this object
    ram_marks.total_marks();
}

Output:

Total Marks are: 215

There can be both abstract and non-abstract methods and variables in an abstract class. Non-abstract methods or variables can not be overridden in the derived classes. For example,

// Creating an abstract class
abstract class Person(){
    // abstract variables
    abstract var first_name:String
    abstract var last_name:String
    abstract var age:Int
 
    // non - abstract method
    fun print_info(){ // Can not be overridden in the derived class
        println("Name of Person: $first_name $last_name ")
        println("Age of Person: $age")
    }
}
 
// derived class
class kid(first_name: String, last_name: String, age: Int) : Person(){
    // overriding all the variables
    override var first_name = first_name;
    override var last_name =last_name;
    override var age = age;
}
 
fun main(){
    // Making an object of kid class
    val ob1 = kid("Ram", "Kumar", 5)
    // Invoking the print_info function
    ob1.print_info();
}

Output:

Name of Person: Ram Kumar
Age of Person: 5

Multiple derived Classes

The same abstract class can be inherited in multiple classes, i.e., one abstract class can have multiple derived classes. Any abstract method or variable of an abstract class can be overridden in all its derived classes. In this program, we override the run function in three derived classes of the Animal.

// Creating an abstract class
abstract class Animal(){
    // abstract variables
   abstract fun run()
}

// Derived Class
class Cat: Animal(){
    override fun run(){
        println("Cat is running")
    }
}
// Derived Class
class Dog: Animal(){
    override fun run(){
        println("Dog is running")
    }
}
// Derived Class
class Horse: Animal(){
    override fun run(){
        println("Horse is running")
    }
}

fun main(){
    // Creating an object of Cat Class
    val ob1= Cat();
    // Invoking the run function associated with the ob1 object
    ob1.run();
    // Creating an object of Dog Class
    val ob2= Dog();
    // Invoking the run function associated with the ob2 object
    ob2.run();
    // Creating an object of Horse Class
    val ob3= Horse();
    // Invoking the run function associated with the ob3 object
    ob3.run();
}

Output:

Cat is running
Dog is running
Horse is running

FAQs

  1. What is an abstract method?
    An abstract method does not have a body, i.e., it can’t be defined inside an abstract class. It is defined inside the derived class of the same abstract class.
     
  2. How is an interface different from an abstract class in Kotlin?
    An interface cannot store a state. They are allowed to have properties, but these must be of abstract nature.
     
  3. What is the default nature of methods in an abstract class?
    All the methods and variables are by default non-abstract. If you want to override these methods and variables inside the derived class, they need to be defined with the open keyword.

Key Takeaways  

Cheers if you reached here!!!

In this blog, we learned about Kotlin Abstract Class. We discussed how an abstract class can be declared and used in a Kotlin program. Additionally, we looked at how the same abstract methods can be overridden in multiple derived classes

After reading this blog, I believe you will be able to work with Kotlin Abstract Classes smoothly. Kotlin Interfaces are quite similar to Kotlin Abstract Classes, check out our other article on the Kotlin Interfaces to learn more about them. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website.

Read some similar articles here:

Live masterclass