Open In App

Kotlin Abstract class

Last Updated : 08 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, an abstract class is a class that cannot be instantiated and is meant to be subclassed. An abstract class may contain both abstract methods (methods without a body) and concrete methods (methods with a body).

An abstract class is used to provide a common interface and implementation for its subclasses. When a subclass extends an abstract class, it must provide implementations for all of the abstract methods defined in the abstract class.

Declaring an Abstract Class

In Kotlin, an abstract class is declared using the abstract keyword in front of the class. An abstract class can not instantiate means we can not create object for the abstract class. 

Abstract class declaration:

abstract class ClassName {
// properties and methods here
}

Key Points to remember: 

  1. We can't create an object for abstract class.
  2. All the variables (properties) and member functions of an abstract class are by default non-abstract. So, if we want to override these members in the child class then we need to use open keyword.
  3. If we declare a member function as abstract then we does not need to annotate with open keyword because these are open by default.
  4. An abstract member function doesn’t have a body, and it must be implemented in the derived class.
  5. An abstract class can contain both abstract and non-abstract members as shown below:
abstract class className(val x: String) {   // Non-Abstract Property

abstract var y: Int // Abstract Property

abstract fun method1() // Abstract Methods

fun method2() { // Non-Abstract Method
println("Non abstract function")
}
}

Example: Abstract and Non-Abstract Members -

Kotlin
abstract class Employee(val name: String) {   // Non-abstract property
    abstract var experience: Int               // Abstract property

    abstract fun salary(): Double              // Abstract method

    fun employeeDetails() {                    // Non-abstract method
        println("Name of the employee: $name")
        println("Experience in years: $experience")
        println("Annual Salary: ${salary()}")
    }

    abstract fun dateOfBirth(date: String)     // Abstract method
}

class Engineer(name: String, override var experience: Int) : Employee(name) {
    override fun salary(): Double {
        return 500000.0
    }

    override fun dateOfBirth(date: String) {
        println("Date of Birth is: $date")
    }
}

fun main() {
    val eng = Engineer("Praveen", 2)
    eng.employeeDetails()
    eng.dateOfBirth("02 December 1994")
}

Output:

Name of the employee: Praveen  
Experience in years: 2
Annual Salary: 500000.0
Date of Birth is: 02 December 1994

Explanation: In the above program, Engineer class is derived from the Employee class. An object eng is instantiated for the Engineer class. We have passed two parameters to the primary constructor while creating it. This initializes the non-abstract properties name and experienceof Employee class. The Then employeeDetails() method is called using the eng object. It will print the values of name, experience and the overridden salary of the employee. In the end, dateOfBirth() is called using the eng object and we have passed the parameter date to the primary constructor. It overrides the abstract fun of Employee class and prints the value of passed as parameter to the standard output.

Overriding Non-Abstract Methods with Abstract Ones

In Kotlin, it’s possible for an abstract class to override a concrete (non-abstract) method from its superclass and make it abstract again. This means the subclass of the abstract class must implement that method.

Example - 

Kotlin
open class LivingThings {
    open fun breathe() {
        println("All living things breathe")
    }
}

abstract class Animals : LivingThings() {
    abstract override fun breathe()
}

class Dog : Animals() {
    override fun breathe() {
        println("Dog can also breathe")
    }
}

fun main() {
    val dog = Dog()
    dog.breathe()
}

Output:

Dog can also breathe

Multiple Subclasses Implementing Abstract Method

An abstract member of an abstract class can be overridden in all the derived classes. In the program, we overrides the cal function in three derived class of calculator.

Example - 

Kotlin
abstract class Calculator {
    abstract fun calculate(a: Int, b: Int)
}

class Add : Calculator() {
    override fun calculate(a: Int, b: Int) {
        println("Addition of two numbers ${a + b}")
    }
}

class Subtract : Calculator() {
    override fun calculate(a: Int, b: Int) {
        println("Subtraction of two numbers ${a - b}")
    }
}

class Multiply : Calculator() {
    override fun calculate(a: Int, b: Int) {
        println("Multiplication of two numbers ${a * b}")
    }
}

class Divide : Calculator() {
    override fun calculate(a: Int, b: Int) {
        println("Division of two numbers ${a / b}")
    }
}

fun main() {
    val add = Add()
    val sub = Subtract()
    val mul = Multiply()
    val div = Divide()

    add.calculate(5, 5)
    sub.calculate(10, 6)
    mul.calculate(10, 12)
    div.calculate(30, 10)
}

Output:

Addition of two numbers 10  
Subtraction of two numbers 4
Multiplication of two numbers 120
Division of two numbers 3

Advantages of using abstract classes in Kotlin:

  1. Abstraction: Abstract classes provide a way to define a common contract between different classes without specifying the implementation details. This enables you to create abstractions that improve the modularity and maintainability of your code.
  2. Polymorphism: Abstract classes allow you to create objects of different types that have the same interface, which enables polymorphic behavior.
  3. Code Reusability: Abstract classes provide a way to reuse code by allowing multiple classes to extend the same abstract class and share the same abstract methods and properties.
  4. Implementing Common Behavior: Abstract classes provide a way to implement common behavior for its subclasses, reducing the amount of code that needs to be written in each subclass.

Disadvantages of using abstract classes in Kotlin:

  1. Limited Instantiation: Abstract classes cannot be instantiated, so they provide limited instantiation details.
  2. Complexity: Abstract classes can make your code more complex, especially if you have many classes that extend multiple abstract classes.

Reference:

A popular book for learning Kotlin is "Kotlin in Action" by Dmitry Jemerov and Svetlana Isakova. This book provides a comprehensive introduction to the Kotlin programming language, including its syntax, libraries, and best practices, with many hands-on examples and exercises.


Next Article
Article Tags :

Similar Reads