Kotlin Abstract class - GeeksforGeeks

28 Mar, 2022
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:
We can’t create an object for abstract class.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.If we declare a member function as abstract then we does not need to annotate with open keyword because these are open by default.An abstract member function doesn’t have a body, and it must be implemented in the derived class.
We can’t create an object for abstract class.
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.
If we declare a member function as abstract then we does not need to annotate with open keyword because these are open by default.
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-
Kotlin
//abstract classabstract 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 classclass 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")}
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.
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 –
Kotlin
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()    lt.breathe()    val d = Dog()    d.breathe()}
Output:
All living things breathe
Dog can also breathe

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 –
Kotlin
// abstract classabstract class Calculator {    abstract fun cal(x: Int, y: Int) : Int}// addition of two numbersclass Add : Calculator() {    override fun cal(x: Int, y: Int): Int {        return x + y    }}// subtraction of two numbersclass Sub : Calculator() {    override fun cal(x: Int, y: Int): Int {        return x - y    }}// multiplication of two numbersclass 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")}
Output:
Addition of two numbers 10
Subtraction of two numbers 4
Multiplication of two numbers 120
Division of two numbers 3

m01nak77
ayushpandey3july
Kotlin OOPs
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
Android UI Layouts
Content Providers in Android with Example
Retrofit with Kotlin Coroutine in Android
How to Change the Color of Status Bar in an Android App?
Android Menus
ImageView in Android with Example
How to Get Current Location in Android?
Kotlin Android Tutorial