Home » Kotlin

Interfaces in Kotlin

This article is related to interfaces in kotlin. In this article we will learn about interfaces, how they are implemented in kotlin and how they differ from interfaces in java?
Submitted by Aman Gautam, on December 28, 2017

Prerequisite: abstract class in kotlin

In java we have studied that interface is a collection of only abstract methods. It cannot contain any function with definition. But, Kotlin added more functionality to the interfaces by enabling us to add code in interfaces. Yeah, really we can add code in interfaces.

Interface can be created using 'interface' keyword.

interface twoWheeler{
   fun getPrice(qt:Int) :Int// abstract method
}

This interface contains an abstract method getPrice(). There is no need of abstract keyword as it is implicitly specified.

In kotlin we can add code to the member functions of interface

interface twoWheeler{
	fun getDeatils(){       //method with code
		println("this is an interface ")
	} 
}

Interfaces are nothing without implementation they must be implemented by some class. They are implemented as like we inherit classes using colon(:) symbol.

class bajaj :twoWheeler{
	override fun getPrice(qt: Int) :Int {
		return qt*45000
	}
}

Here, override keyword is used to implement method of interface and this is compulsory in kotlin.

Interface can contain properties, see the example

interface twoWheeler{
	var price :Int
}

If we initialize the property price like,

var price:Int=45000   // this will produce errror

Properties need to be overridden in the sub class as per the requirement. Use override keyword for that.

class bajaj : twoWheeler{
override var price=45000
}

Program to implement interface in kotlin

interface twoWheeler {
    //method with code
	fun getDeatils() {       
        println("This is an interface providing a method getPrice() which returns price as per quantity ")
    }

    var price: Int                              // property
    fun getPrice(qt: Int): Int                 // abstract method
}

class bajaj : twoWheeler {
    override var price = 45000
    override fun getPrice(qt: Int): Int {
        return qt * price
    }
}

class hero : twoWheeler {
    override var price = 50000
    override fun getPrice(qt: Int): Int {
        return (qt * price + 4500)
    }
}

fun main(args: Array<String>) {
    var platina = bajaj()
    platina.getDeatils()
    println("Enter quantity for platina  : ")
    var qt = readLine()!!.toInt()
    print(" Your Price : ")
    println(platina.getPrice(qt))

    var passion = hero()
    println("Enter quantity for passion  : ")
    qt = readLine()!!.toInt()
    print(" Your Price : ")
    println(passion.getPrice(qt))
}

Output

This is an interface providing a method getPrice() which returns price as per quantity 
Enter quantity for platina  :
1
 Your Price : 45000
Enter quantity for passion  :
1
 Your Price : 54500

Through interfaces we can also achieve multiple inheritance

While implementing multiple interfaces a problem may occur that multiple interfaces may contain same function name and complier might confuse while calling them. So to remove ambiguity when calling function with same name, we have to use super keyword then angle braces "<interfaceName>" and then function. As seen in below example.

Program to implement multiple interface in Kotlin

interface A {
    fun display() {
        println("this is interface A")
    }
}

interface B {
    fun display() {
        println("this is interface B")
    }
}

class C : A, B {
    override fun display() {
        super<A>.display()
        super< B>.display()

    }
}

fun main(args: Array<String>) {
    var ob = C()
    ob.display()
}

Output

this is interface A
this is interface B





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.