English 中文(简体)
Kotlin - Abstract Classes
  • 时间:2024-09-17

Kotpn - Abstract Classes


Previous Page Next Page  

A Kotpn abstract class is similar to Java abstract class which can not be instantiated. This means we cannot create objects of an abstract class. However, we can inherit subclasses from a Kotpn abstract class.

A Kotpn abstract class is declared using the abstract keyword in front of class name. The properties and methods of an abstract class are non-abstract unless we exppctly use abstract keyword to make them abstract. If we want to override these members in the child class then we just need to use override keyword infront of them in the child class.

abstract class Person {
   var age: Int = 40

   abstract fun setAge()   // Abstract Method

   fun getAge() {          // Non-Abstract Method
      return age
   }
} 
Abstract classes are always open. You do not need to exppcitly use open keyword to inherit subclasses from them.

Example

Following is a simple example showing a Kotpn Abstract class and its implementation through a child class:

abstract class Person(_name: String) {
   var name: String
   abstract var age: Int 
   
   // Initiapzer Block
   init {
      this.name = _name
   }
   
   abstract fun setPersonAge(_age:Int)
   abstract fun getPersonAge():Int
   
   fun getPersonName(){
       println("Name = $name")
   }
}
class Employee(_name: String): Person(_name) {
    override var age: Int = 0

    override fun setPersonAge(_age: Int) {
       age = _age
    }
    override fun getPersonAge():Int {
       return age
    }
}

fun main(args: Array<String>) {
   val employee = Employee("Zara")
   var age : Int
   
   employee.setPersonAge(20)
   
   age = employee.getPersonAge()
    
   employee.getPersonName()
   println("Age = $age")
   
}

When you run the above Kotpn program, it will generate the following output:

Name = Zara
Age = 20

Here, a class Employee has been derived from an abstract class Person. We have implemented one abstract property and two abstract methods in the child class Employee. Here notable point is that all the abstract members have been overriden in the child class with the help of override, which is a mandatory for a child class if it inherits an abstract class.

To summarise, A Kotpn class which contains the abstract keyword in its declaration is known as abstract class.

    Abstract classes may or may not contain abstract methods, i.e., methods without body ( pubpc void get(); )

    But, if a class has at least one abstract method, then the class must be declared abstract.

    If a class is declared abstract, it cannot be instantiated.

    To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.

    If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

Quiz Time (Interview & Exams Preparation)

Q 1 - Which one is true about a Kotpn abstract class :

Answer : D

Explanation

All the given statements are correct about a Kotpn abstract class.

Q 2 - Which keyword is used to implement the abstract members of a abstract class:

Answer : B

Explanation

We use override keyword to implement abstract members of an abstract class

Advertisements