Implementation of Interface
A class or an object can implement an interface. The class implementing an interface must provide definitions for all the methods and variables of the interface. To implement an interface in a class, you can simply use the class name followed by a colon and then the interface's name. For example,
class Dog: Animal
Example
// Creating an interface Animal
interface Animal {
fun run()
}
// creating the Dog class based on the Animal Interface
class Dog: Animal {
// overriding the run function
override fun run(){
println("Dog is running")
}
}
fun main(){
// creating an object of Dog Class
val ob = Dog();
// invoking the run function
ob.run();
}
Output:
Dog is running
Here, we created an interface Animal which was then implemented by the Dog Class. Dog class provided the implementation of the run function.
Default Methods and Default Values
In interface, parameters of a method can have default values which will be used in case the value of that variable is not provided while invoking that method. Also, the methods in an interface can have default implementations which will be used in case the class implementing the interface doesn't override that method. For example,
// Creating an interface Calculator
interface Calculator {
// function with default value of parameter y as 5
fun multiply(x: Int, y: Int = 5 )
// function with default implementation
fun subtract( x: Int, y: Int){
println("$x - $y = ${x-y}" )
}
}
// creating the foo class based on the Calculator Interface
class foo: Calculator {
// overriding the multiply function
override fun multiply(x: Int, y: Int){
println("$x * $y = ${x*y}")
}
// Not overriding the subtract method.
// Hence its default implementation from the interface will be used.
}
fun main(){
// creating an object of foo Class
val ob = foo();
// invoking the multiply function
ob.multiply(10);
// invoking the subtract function
ob.subtract(50, 14)
}
Output:
10 * 5 = 50
50 - 14 = 36
Here the multiply method of the Calculator interface has a default value of 5 for the parameter y. A default implementation of the subtract function is also provided by the Calculator interface.
Inheriting Interfaces
An interface can also be inherited by another interface in Kotlin. An interface inheriting another interface can add its own characteristics and methods. More than one interface can be extended by an interface, and the implementing types will have to provide the definition for all the methods and properties of that interface and of all the interfaces it inherited from. For example,
// Creating an interface Parameters
interface Parameters {
val a: Int
val b: Int
}
// Inheriting Parameters interface
interface Calculator: Parameters {
fun add()
fun subtract()
fun multiply()
fun division()
}
// Creating the class foo based on Interface Calculator
class foo: Calculator{
// overriding the values of a and b
override val a : Int = 40
override val b : Int = 10
// overriding add method
override fun add(){
println("$a + $b = ${a+b}")
}
// overriding subtract method
override fun subtract(){
println("$a - $b = ${a-b}")
}
// overriding multiply method
override fun multiply(){
println("$a * $b = ${a*b}")
}
// overriding division method
override fun division(){
println("$a / $b = ${a/b}")
}
}
fun main(){
// creating an object of foo Class
val ob = foo();
// Invoking the add function of foo class
ob.add()
// Invoking the subtract function of foo class
ob.subtract()
// Invoking the multiply function of foo class
ob.multiply()
// Invoking the division function of foo class
ob.division()
}
Output:
40 + 10 = 50
40 - 10 = 30
40 * 10 = 400
40 / 10 = 4
Here, the interface Parameters is inherited by the interface Calculator. The class foo implementing the Calculator interface overrides all the methods and variables defined in the Parameters and the Calculator interface.
FAQs
-
Can an interface inherit more than one interface?
Yes, an interface in Kotlin can extend any number of interfaces.
-
Can we provide default values to variables of an interface?
Yes, default values can be provided to variables of an interface that will be used in case the implementing type doesn't override that variable.
-
Can the implementing type not provide an implementation of some methods of its associated interface?
Yes, the implementing type has to provide the implementation of all the methods defined in its interface. It may skip some implementation of properties and methods which have a default implementation in its associated interface.
Key Takeaways
Cheers if you reached here!!!
In this blog, we learned about Kotlin Interfaces. We discussed how an interface can be declared and used in a Kotlin program. Additionally, we looked at how an interface can be inherited by other interfaces.
After reading this blog, I believe you will be able to work with Kotlin Interfaces smoothly. If you wish to learn about Kotlin Loops in detail, you can check out our other article on Kotlin Loops. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website.