Kotlin Interfaces
An interface is a type that defines a set of methods and properties that a class can implement. It provides a way to define a contract between the implementing class and the calling code. An interface can be thought of as a blueprint for a class, where the class must implement all the methods and properties defined in the interface.
Creating An Interface
This is how the basic syntax of an interface can be created:
interface Name {
// Properties and functions go here in the body.
}
Example
In the following code example, an interface named MyListener is defined and a class named Name which inherits it:
fun main() {var myObject = Name()println(myObject.name)myObject.name ="XYZ"println(myObject.name)myObject.EmptyFn()}interface Mylistener {var name: String // The normal properties are by default abstract in nature it cannot be given a body.fun jump() {print("World")} // The function's body shows that its `open` in nature.fun EmptyFn() // Methods are by default `open` and `public` in nature so it can give the body for the function or call it in other class and override it.}class Name : Mylistener {override var name: String = "Hello"override fun EmptyFn() {println("Override Text")}override fun jump() {} // Optional to override.}
In the code above, the Mylistener interface declares a property and two functions:
nameis a read-write property of typeString.jump()is a function with a default implementation that prints the text"World".EmptyFn()is a function without a body.
The Name class implements the Mylistener interface and overrides its two functions and property. The name property is initialized to "Hello" and is made writable in the Name class. The EmptyFn() function is also overridden with a new implementation that prints the text "Override Text". The jump() function is optional to override but is done so here, so the print("World") in the interface will not run.
The output for the above code will be:
HelloXYZOverride Text
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours