Lesson 35: declaration and implementation of interfaces in Kotlin

Lesson 35: declaration and implementation of interfaces in Kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

An interface declares a series of methods and properties that must be implemented later by one or more classes, also an interface in Kotlin may have implemented methods.

The interfaces come to replace the impossibility of multiple inheritances in Kotlin.

It uses the same syntax as inheritance to indicate that a class implements an interface.

For example, we can have two classes that represent an airplane and a helicopter. Then raise an interface with a method called fly. The two classes can implement this interface and code the fly method (algorithms are surely different but the behavior of flying is common to both an airplane and a helicopter)

The syntax in Kotlin for declaring an interface is:

Interface [name of the interface] {
    [Declaration of properties]
    [Method declaration]
    [Method implementation]
}

Problem 1

Define an interface called Point that declares a method called print. Then declare two classes to implement it.

Project142 – Principal.kt

interface Point {
    fun print ()
}

class plane (val x: Int, val y: Int): Point {
    override fun print () {
        println ("Point in the plane: ($x, $y)")
    }
}

class space (val x: Int, val y: Int, val z: Int): Point {
    override fun print () {
        println ("Point in space: ($x, $y, $z)")
    }
}

fun main (parameter: Array <String>) {
    val plane1 = plane (10, 4)
    plane1.print ()
    
    val space1 = space (20, 50, 60)
    space1.print ()
}

To declare an interface in Kotlin, we use the interface keyword and then its name. Then between braces, we indicate all the headers of methods, properties and methods already implemented. In our example, we declare the interface Point and we indicate that the one who implements it must define a method called print without parameters and that does not return anything:

Interface Point {
    Fun print()
}

On the other hand, we declare two classes called plane, with two properties and space with three properties, in addition, we indicate that these classes will implement the interface Point:

Class plane (val x: Int, val y: Int): Point {
    Override fun print () {
        Println ("Point in the plane: ($ x, $ y)")
    }
}

Class Space (val x: Int, val y: Int, val z: Int): Point {
    Override fun print () {
        Println ("Point in space: ($x, $y, $z)")
    }
}

The syntax to indicate that a class implements an interface is equal to inheritance. If one class inherits from another it can also implement one or more interfaces by comma separating each of the interfaces.

The print method in each class is implemented in a different way, one prints 3 properties and the other prints 2 properties.

Then we define in the main function an object of class plane and another one of type Space:

Fun main (parameter: Array <String>) {
    Val plane1 = plane(10, 4)
    plane1.print ()
    Val Space1 = Space (20, 50, 60)
    Space1.print ()
}

Problem 2

It has the following interface:

Interface Figure {
    Fun calculateSurface (): Int
    Function calculatePerimeter (): Int
    Fun titleResult () {
        Println ("Data of the figure")
    }
}

Define two classes that represent a Square and a Rectangle. Implement the Figure interface in both classes.

Project143 – Principal.kt

interface figure {
    fun calculateSurface (): Int
    fun calculatePerimeter (): Int
    fun titleResult () {
        println ("Data of the figure")
    }
}

class Square (val side: Int): figure {
    override fun calculateSurface (): Int {
        return side * side
    }

    override fun calculatePerimeter (): Int {
        return side * 4
    }
}

class Rectangle (val loadMajor: Int, val loadMinor: Int): figure {
    override fun calculateSurface (): Int {
        return loadMajor * loadMinor
    }

    override fun calculatePerimeter (): Int {
        return (loadMajor * 2) + (loadMinor * 2)
    }
}

fun main (parameter: Array <String>) {
   val Square1 = Square (10)
    Square1.titleResult ()
    println ("Perimeter of the square: ${Square1.calculatePerimeter ()}")
    println ("Square surface: ${Square1.calculateSurface ()}")

    val rectangle1 = Rectangle (10, 5)
    rectangle1.titleResult ()
    println ("Rectangle perimeter: ${rectangle1.calculatePerimeter ()}")
    println ("Square surface: ${rectangle1.calculateSurface ()}")
}

In this problem the interface Figure has two abstract methods that must be implemented by the classes and a concrete method, that is to say already implemented:

Interface Figure {
    Fun calculateSurface (): Int
    Function calculatePerimeter (): Int
    Fun titleResult () {
        Println ("Data of the figure")
    }
}

The Square class indicates that it implements the Figure interface, this makes it necessary to implement the methods CalculateSurface and calculatePerimeter:

Class Square (val side: Int): Figure {
    Override fun calculateSurface (): Int {
        Return side * side
    }

    Override fun calculatePerimeter (): Int {
        Return side * 4
    }
}

Similarly, the Rectangle class implements the interface Figure:

Class Rectangle (val sideMajor: Int, val sideMinor: Int): Figure {
    Override fun calculateSurface (): Int {
        Return loadMajor * loadMinor
    }

    Override fun calculatePerimeter (): Int {
        Return (loadMajor * 2) + (loadMinor * 2)
    }
}

In the main function, we define an object of the class Square and another of the class Rectangle, then we call the methods titleResult, calculatePerimeter and calculateSurface for each object:

Fun main (parameter: Array <String>) {
    Square value1 = Square (10)
    Square1.titleResult ()
    Println ("Perimeter of the square: ${square1.calculatePerimeter ()}")
    Println ("Square surface: ${square1.calculateSurface ()}")
    Val rectangle1 = Rectangle (10, 5)
    Rectangle1.titleResult ()
    Println ("Rectangle perimeter: ${rectangle1.calculatePerimeter ()}")
    Println ("Square surface: ${rectangle1.calculateSurface ()}")
}

Acotaciones

A method or function can receive as an interface parameter. Then we can pass objects of different classes that implement this interface:

Interface Figure {
    Fun calculateSurface (): Int
    Function calculatePerimeter (): Int
    Fun titleResult () {
        Println ("Data of the figure")
    }
}

Class Square (val side: Int): Figure {
    Override fun calculateSurface (): Int {
        Return side * side
    }

    Override fun calculatePerimeter (): Int {
        Return side * 4
    }
}

Class Rectangle (val loadMajor: Int, val loadMinor: Int): Figure {
    Override fun calculateSurface (): Int {
        Return loadMajor * loadMinor
    }

    Override fun calculatePerimeter (): Int {
        Return (loadMajor * 2) + (loadMinor * 2)
    }
}

Fun print (fig: Figure) {
    Println ("Perimeter: ${fig.calculatePerimeter ()}")
    Println ("Surface: ${fig.calculatesurface ()}")
}

fun main (parameter: Array <String>) {
val Square1 = Square (10)
Square1.titleResult ()
println (“Perimeter of the square: ${Square1.calculatePerimeter ()}”)
println (“Square surface: ${Square1.calculateSurface ()}”)

val rectangle1 = Rectangle (10, 5)
rectangle1.titleResult ()
println (“Rectangle perimeter: ${rectangle1.calculatePerimeter ()}”)
println (“Square surface: ${rectangle1.calculateSurface ()}”)
}

The function print receives as parameter fig that is of type Figure:

Fun print (fig: Figure) {
    Println ("Perimeter: ${fig.calculatePerimeter ()}")
    Println ("Surface: ${fig.calculateSurface ()}")
}

In the main we can call the print function by passing both the square object1 and rectangle1:

    Print (square1)
    Print (rectangle1)	

This is possible since both objects their classes implement the Figure interface.

About The Author

Related posts

Leave a Reply