Trending October 2023 # How Interface Work In Kotlin With Examples? # Suggested November 2023 # Top 16 Popular |

Trending October 2023 # How Interface Work In Kotlin With Examples? # Suggested November 2023 # Top 16 Popular

You are reading the article How Interface Work In Kotlin With Examples? updated in October 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Interface Work In Kotlin With Examples?

Introduction to Kotlin Interface

Web development, programming languages, Software testing & others

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


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( }


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


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, { print("x = $x, j = $j, ") val res = x + j println("res = $res") res }) val ex2 = ls.fold(6, Int::times) println("The mentioned first input is ex1 = $ex1") println("The mentioned second input is ex2 = $ex2") return f("Have a Nice Day users", 12) } val res = demo3(ex3) println("Please try again = $res") }


Example #3


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 println("Welcome To My Domain its the third example that related to the kotlin interface") val ob = Sample() ob.demo() ob.demo1() }


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.


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 –

You're reading How Interface Work In Kotlin With Examples?

Update the detailed information about How Interface Work In Kotlin With Examples? on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!