Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - ABSTRACT CLASS


An abstract class in Kotlin is a class which cannot be instantiated and can have methods which are not defined.



  1. To make a class abstract, we need to place the abstract keyword just before the class definition.

  2. And as said above, there can be one or more undefined method which must have abstract keyword before the method declaration.

  3. We cannot create objects of an Abstract class.

  4. The classes inheriting an abstract class has to define the undefined methods(i.e. the abstract methods).

We will clear the above points with the example :


Story :

The supreme god decided that in order to communicate, every living being should make some sound. i.e Humans will talk, dogs will bark, cats meao e.t.c.

Now, there was no way he could define the sound behavior as it will be different for all living beings. So he made this rule that every living being should have a sound behavior but the sound they makes will be determined by their own gods.

And thus the class was prepared by Master God:


abstract class LivingBeing(airTemp: String) {

    var air: String = airTemp

    fun breathe() {
	    println("Breathes "+air)
    }

    abstract fun sound() // The sound method is incomplete.
}

Now we can see the abstract keyword used in two places:

  1. Before the sound() behaviour(As it is incomplete).

    abstract fun sound()

  2. Before the LivingBeing class(Since sound() is incomplete, the class is incomplete as well).

    abstract class LivingBeing

So, from the above definition it is pretty clear that if a method/behaviour is not defined it should be marked as abstract.


Thus the LivingBeing class is also Abstract because it has an abstact behaviour(Incomplete behaviour).


And since the class is incomplete we cannot create any objects out of it.


In other words abstract classes cannot be instantiated.


Then, what is the purpose of this class from which we cannot create any objects? We will see it below:


Human Being Creator


Example :



abstract class LivingBeing(airTemp: String) {

    var air: String = airTemp

    fun breathe() {
        println("Breathes "+air)
    }

    abstract fun sound() // The sound method is incomplete.
}

class Human(var nme: String, var fd: String, var lang: String, var airTemp: String): LivingBeing(airTemp) {

    var name: String = nme
    var food: String = fd
    var language: String = lang


    fun eat() {
        println("Eats "+food)
    }

    fun speak() {
        println("Speaks "+language)
    }

    override fun sound(){
        println("Humans will speak") // This method is defined.
    }
}

fun main() {

    var human = Human("Rakhi", "Rice", "Hindi", "Oxygen")

    human.eat()
    human.speak()
    human.sound()
    human.breathe()
}


Output :



  Eats Rice
  Speaks Hindi
  Humans will speak
  Breathes Oxygen

So, in the above example, we have the Abstract class LivingBeing,


abstract class LivingBeing(airTemp: String) {

	var air: String = airTemp

	fun breathe() {
		println("Breathes "+air)
	}

	abstract fun sound() // The sound method is incomplete.
}

The Abstract class LivingBeing has two methods/behaviours, breathe() and sound(). Outof which only breathe() method is defined.


And the sound() method is undefined or Abstract.


Now the sound() method would be defined in the Human class where the Abstract class LivingBeing would be inherited.


class Human(var nme: String, var fd: String, var lang: String, var airTemp: String): LivingBeing(airTemp) {

	var name: String = nme
	var food: String = fd
	var language: String = lang

	fun eat() {
		println("Eats "+food)
	}

	fun speak() {
		println("Speaks "+language)
		}

	override fun sound(){
		println("Humans will speak") // This method is defined.
	}
}

And in the Human class, we have defined the sound() method(Using the override keyword).


override fun sound(){
	println("Humans will speak")
}

Story :

So, the Human creator god is bound to define the sound() method/behaviour as he is inheriting the LivingBeing class. Naturally the purpose of the supreme god was solved. As he wanted all living beings should make some sound. What kind of sound? Will be determined by their own Gods.

If the Human creator god wouldn't have defined the sound() method/behaviour the code would have ended up with some kind of error.


override fun sound(){
	println("Humans will speak")
}

Note :There is no hard and fast rule that at least one method has to be abstract in an abstract class. An abstract class can contain all methods which are defined(i.e. non abstract methods) but if there is at least one method in a class marked abstract, the class should be abstract as well.