Updated March 30, 2023
Introduction to Kotlin Interface
The kotlin interface is one of the ways that can be used to declare the methods without body, and it can be accepted for both abstract and non-abstract methods so that it contains the definition of abstract methods as well as implementations of non-abstract methods; the interface keyword is used to define the interfaces it can be implemented by the class or an object and an interface the name of the custom type is followed by the operator like colon and the name of the interface which is to be implemented by the child classes also the methods can be overridden.
Syntax of Kotlin Interface
In kotlin language, classes, interfaces, methods and other default keywords for to implement the application. The interfaces can have the implementation of the functions, and it can have non-abstract properties by defining their getters and setters method.
interface name{
fun demo(): datatype
var variable: datatype
---some logic codes depends on the requirement----
}
class name1:name
{
override var variable
override fun demo(): datatype{
--some logic codes—
}
}
fun main()
{
var v = name1()
v.demo()
}
The above codes are the basic syntax for to utilising the interface on the kotlin codes. Here we declared an interface, and it can be implemented using the class.
How does Interface Work in Kotlin?
The kotlin interface contains the definition of functions and properties; it’s also a custom type, and it is provided that cannot be instantiated directly through the code. However, with the interface, we can define the set of properties and methods that the concrete types must be followed and implemented. Generally, the interface definition in the kotlin language begins with the interface keyword. It is followed by the interface name along with the operators like curly braces within which the member of the interface resides on the main function. The main difference is that the members will have no definition of their own language.
These definitions will be provided by the conforming types a class or an object can implement the interface. When we implement the interface, the conforming type must be provided with the definition for all of its members. To implement an interface, the name of the custom type is followed by the colon operator and the name of the interface which is to be implemented. The default values will be passed as the parameters in the interface methods; if the parameter is not provided at the function call, the default value is used. So the methods have the default implementations; these are used in the case where the method is not to be overridden.
Examples of Kotlin Interface
Given below are the examples of Kotlin Interface:
Example #1
Code:
interface Sample {
val x : Int
val y : String
get() = "Welcome To My Domain its the first example that related to the kotlin interface"
}
interface Demo {
fun Sample1()
}
class Example : Sample, Demo {
override val x : Int
get() = 41
override fun Sample1()
{
println("Have a Nice Day users please try again")
}
private val nm = "Sample Example"
fun eg() = "This is the first example we discussed regarding kotlin interface"
}
class NewEmployees
{
var ename: String = ""
var eid: Int = 0
var sex: Char = 'M'
var salary: Double = 0.toDouble()
fun enteredNewEmployees(n1: String, q: Int, gen: Char, sal: Double) {
ename = n1
eid = q
sex = gen
salary = sal
println("Please see the below new employee name: $ename")
println("Please see the NewEmployees id: $eid")
println("Your sex is: $sex")
println("The Cross salary of the new employees is : $salary")
}
fun insertName(n1: String) {
this.ename = n1
}
}
fun main()
{
val ob = Example()
ob.Sample1()
var ob1 = NewEmployees()
var ob2 = NewEmployees()
ob1.enteredNewEmployees("Siva", 41, 'M', 30000.00)
ob2.insertName("Raman")
println("ename of the new NewEmployees: ${ob2.ename}")
val s = Example()
println(s)
println(s.eg())
}
Output:
In the above example, we used interfaces and classes to implement the interfaces and their methods. The class also overrides the interface’s methods.
Example #2
Code:
interface dime {
val len : Double
val bre : Double
val heigh : Double
}
interface operation : dime {
fun demo1()
fun demo2()
}
class Example : operation {
override val len : Double
get() = 23.0
override val bre : Double
get()= 33.0
override val heigh : Double
get() = 43.0
override fun demo1()
{
println("demo1 is ${len * bre * heigh}")
}
override fun demo2()
{
println("demo2 is ${3*(len+bre+heigh)}")
}
}
fun main()
{
val ob = Example()
ob.demo1()
ob.demo2()
val ls = listOf(6, 12, 18, 24, 30)
ls.fold(0, {
x: Int, j: Int ->
print("x = $x, j = $j, ")
val res = x + j
println("res = $res")
res
})
val ex1 = ls.fold("Your input Elements are:", { x, j -> x + " " + j })
val ex2 = ls.fold(6, Int::times)
println("The mentioned first input is ex1 = $ex1")
println("The mentioned second input is ex2 = $ex2")
val ex3: String.(Int) -> String = { p -> this.repeat(p) }
val ex4: (String, Int) -> String = ex3
fun demo3(f: (String, Int) -> String): String {
return f("Have a Nice Day users", 12)
}
val res = demo3(ex3)
println("Please try again = $res")
}
Output:
In the second example, we used interfaces with the kotlin collections and their default methods for to implement the application.
Example #3
Code:
interface Third {
fun demo() {
println("Thank you for declaring the first interface with the method")
}
}
interface Exam {
fun demo1() {
println("Its the second interface for declaring the method and this kotlin codes")
}
}
class Sample: Third, Exam
fun main(args: Array<String>) {
println("Welcome To My Domain its the third example that related to the kotlin interface")
val ob = Sample()
ob.demo()
ob.demo1()
}
Output:
In the final example, we used to declare the two interfaces and the methods we can implement using the class. But the interface methods are not overridden.
Conclusion
In kotlin language, we used different concepts and their features to implement the application. Among that interfaces is the main role for to reduce the coding complexity, and even though the programmer will implement the specific interface methods by using the classes, it depends upon the user and project requirement.
Recommended Articles
This is a guide to Kotlin Interface. Here we discuss the introduction, syntax, and working of the interface in Kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –