Skip to content
Related Articles

Related Articles

Improve Article
Kotlin Abstract class
  • Last Updated : 23 Jun, 2020

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

Abstract class declaration:

abstract class className {

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.

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")

Kotlin program of using both abstract and non-abstract members in an abstract class-

//abstract class
abstract class Employee(val name: String,val experience: Int) {   // Non-Abstract
                                                                  // Property
    // Abstract Property (Must be overridden by Subclasses)
    abstract var salary: Double
    // Abstract Methods (Must be implemented by Subclasses)
    abstract fun dateOfBirth(date:String)
    // Non-Abstract Method
    fun employeeDetails() {
        println("Name of the employee: $name")
        println("Experience in years: $experience")
        println("Annual Salary: $salary")
// derived class
class Engineer(name: String,experience: Int) : Employee(name,experience) {
    override var salary = 500000.00
    override fun dateOfBirth(date:String){
        println("Date of Birth is: $date")
fun main(args: Array<String>) {
    val eng = Engineer("Praveen",2)
    eng.dateOfBirth("02 December 1994")


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

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 a non-abstract open member with an abstract one –

In Kotlin we can override the non-abstract open member function of the open class using the override keyword followed by an abstract in the abstract class. In the below program we will do it.

Kotlin program of overriding a non-abstract open function by an abstract class –

open class Livingthings {
    open fun breathe() {
        println("All living things breathe")
abstract class Animal : Livingthings() {
    override abstract fun breathe()
class Dog: Animal(){
    override fun breathe() {
        println("Dog can also breathe")
fun main(args: Array<String>){
    val lt = Livingthings()
    val d = Dog()


All living things breathe
Dog can also breathe

Multiple derived classes –

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.

Kotlin program of overriding the abstract function in more than one derived class –

// abstract class
abstract class Calculator {
    abstract fun cal(x: Int, y: Int) : Int
// addition of two numbers
class Add : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x + y
// subtraction of two numbers
class Sub : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x - y
// multiplication of two numbers
class Mul : Calculator() {
    override fun cal(x: Int, y: Int): Int {
        return x * y
fun main(args: Array<String>) {
    var add: Calculator = Add()
    var x1 =, 6)
    println("Addition of two numbers $x1")
    var sub: Calculator = Sub()
    var x2 =,6)
    println("Subtraction of two numbers $x2")
    var mul: Calculator = Mul()
    var x3 =,6)
    println("Multiplication of two numbers $x3")


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :