Kotlin Abstract class

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 annotatate 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-

filter_none

edit
close

play_arrow

link
brightness_4
code

//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.employeeDetails()
    eng.dateOfBirth("02 December 1994")
}

chevron_right


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 initialize 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 open class using the override keyword followed by 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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

open class Livingthings {
    open fun breathe() {
        println("All living things breath")
    }
}
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()
    lt.breathe()
    val d = Dog()
    d.breathe()
}

chevron_right


Output:

All living things breath
Dog breathe also

Multiple derived classes –

An abstract member of abstract class can be override 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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = add.cal(4, 6)
    println("Addition of two numbers $x1")
    var sub: Calculator = Sub()
    var x2 = sub.cal(10,6)
    println("Subtraction of two numbers $x2")
    var mul: Calculator = Mul()
    var x3 = mul.cal(20,6)
    println("Multiplication of two numbers $x3")
}

chevron_right


Output:

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.