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




KOTLIN - INTERFACE


So, in the previous tutorial of Abstract Class we have learnt if there is at-least one abstract method in a class the entire class has to marked as abstract.


That is the concept of abstract class.


So in an abstract class there can be abstract(undefined) as well as non abstract(defined) methods.


Now the concept of Interface takes us one step ahead of abstraction.


i.e In an Interface there can be abstract(undefined) as well as non abstract(defined) methods. Just that in an Interface the properties should also be abstract. In simple words the properties cannot be instantiated.


Let us take the abstract class LivingBeing from the previous tutorial.


abstract class LivingBeing(airTemp: String) {

	var air: String = airTemp

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

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

In the abstract class LivingBeing, we have a property air that is initialised,


var air: String = airTemp

But in case of an Interface, the property cannot be initialised and cannot have a constructor. And rest all are same as an Abstract Class.


Note : Just like a class is declared with 'class' keyword. Similarly an interface is declared with 'interface' keyword.

Let us see interface with the below example.


Example :



interface LivingBeing(airTemp: String) {

    var air: String = airTemp

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

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


Output :



  An interface may not have a constructor

So, we have defined an interface with a constructor,


interface LivingBeing(airTemp: String)

And ended up with an error,


An interface may not have a constructor

Similarly, if we want to initialise a property of an interface with a value, it ends up with an error.


Example :



interface LivingBeing {

    var air: String = "Oxygen"

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

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


Output :



  Property initializers are not allowed in interfaces

Now, let us write the correct code on, how to implement an interface.


Example :



interface LivingBeing {

    var air: String // The property is abstract by default.

    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 {

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

    override var air: String = airTemp
    
    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, as said an interface would have the property that would be abstract by default.


And we have the interface, LivingBeing,


interface LivingBeing {

	var air: String // The property is abstract by default.

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

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

So, in the interface LivingBeing, we have the abstract property air that would be defined in the concrete class that inherits the interface LivingBeing.


var air: String

And there are two methods breathe() which is non abstract and sound() which is abstract.


Then we have the class Human where we have inherited the interface LivingBeing.


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

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

	override var air: String = airTemp

	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 initialised the property air, by overriding it.


override var air: String = airTemp

And we get the final output,


Eats Rice
Speaks Hindi
Humans will speak
Breathes Oxygen