Kotlin - Abstract class

A class which is declared with abstract key word is called abstract class. Abstract class contains both abstract and non abstract methods.

What is abstract?

abstraction is a technique which is hide the implementations and show only declaration to out side.

abstract class Employee {
    
    var sal: Int = 40000

    fun displaySal(sal: Int) {
        println("My Salary is sal.")
    }

    abstract fun displayJob(description: String)
}

Here, Employee is abstract class, we cann't create object for the Employee class. It contains displyaSal() non abstract functions and displayJob() abstract function. Note: Abstract classes are always open. No need to explicitly use open keyword to inherit subclasses from them

abstract class Company(name: String) {
    init {
        println("Employee name is $name.")
    }
    fun displaySal(sal: Int) {
        println("Eployee Salary is $sal.")
    }
    abstract fun displayJob(description: String)
}
class Employee(name: String): Company(name) {
    override fun displayJob(description: String) {
        println(description)
    }
}
fun main(args: Array) {
    val emp = Employee("Chandramouli")
    emp.displayJob("Chandramouli is a Team leader")
    emp.displaySal(60000)
}

Output
Employee name is Chandramouli
Chandramouli is a Team leader
Eployee Salary is 60000

Subscribe For Daily Updates