API Interface

Learn how to write interfaces that perform HTTP operations to consume data from REST APIs.

Interfaces in architectures

Interfaces are blueprints of a class. In MVVM architecture, interfaces provide 100% abstraction since they only provide method signatures and fields without actual implementation. This serves as a contract between a programmer and the program they’re working with: all methods defined must be implemented.

Interfaces in Kotlin and Java 8 can have both abstract and non-abstract methods that can be implemented. However, non-abstract properties aren’t permitted.

Let’s look at an example.

Press + to interact
interface MyInterface {
fun countryOfOrigin():String //abstract method
val age: Int //abstract property
//Non abstract properties are not allowed
fun verifyPassport() { //non abstract method with a body
println("Passport Verified, Welcome to Kenya")
}
}
class InterfaceImp : MyInterface {
// implementation of abstract methods and properties
override fun countryOfOrigin() = "SouthAfrica"
override val age: Int = 25
}
fun main(args: Array<String>) {
val obj = InterfaceImp()
//Calling implemented methods of the interface
print("My country of origin is : ")
println(obj.countryOfOrigin())
print("age = ")
println(obj.age)
print("Passport status : ")
obj.verifyPassport()
}

Interfaces and Object Oriented Programming (OOP) in architecture allow us to choose which class implements which method. Different classes implement the same method in multiple ways, offering access to different kinds of implementations through the same interface, which is known as polymorphism.

Interfaces in HTTP protocols

This represents methods that represent possible API calls.

They have ...