Kotlin Interfaces With Example



  • We can group classes based on what they are i.e. based on inheritance. The circle is a shape, Square is a shape - these classes belong to the Shape hierarchy because they are Shapes.
  • We can also group classes based on what they do or what behavior they exhibit. A circle object can be dragged, a person object can be dragged - here we have grouped classes Circle and Person based on the behavior dragging.
  • Even if the classes do not belong to a single class hierarchy, we can group them based on the behavior. This is where Interfaces come into the picture.

Interface - 

  • When you want your class to exhibit certain behavior irrespective of the class hierarchy they belong to - Use Interface. Interfaces help you to implement polymorphism based on the behavior defined in the interface.
  • Methods defined in the interfaces are 100% abstract but in Kotlin you can also define methods that are non-abstract i.e. methods can have a body. Non-abstract methods in interfaces do not have any state they can rely on - they are just simple functions.
  • A class that implements an interface has the responsibility of providing the body of the abstract members.

interface Draggable
{
    fun drag()
}

class Circle: Draggable
{
    override fun drag() = println("Circle is dragging")
}

class Person: Draggable
{
    override fun drag() = println("Person is dragging")
}

Explanation - 
  • Here we have defined an interface named Draggable. It has one abstract method by the name drag. Now whoever implements this interface has the responsibility of providing the body of this method.
  • Child classes Circle and Person implements Draggable and implements the drag method based on their own functionality. The draggable interface does not care how the child classes define the method. It just makes sure that everyone who implements this interface, provides the body of the abstract methods defined inside the interface.

Advantages of Interfaces 

Polymorphism loves interfaces. When we have classes from different class hierarchies, interfaces help in implementing polymorphism. Let's take one example - 

fun dragObjects(list: Array<Draggable>)
{
    for(obj in list)
    {
        obj.drag()
    }
}

fun main()
{
    dragObjects(arrayOf(Circle(), Person()))
}

Explanation - 
  • Drag Objects function takes an array of objects that implement this interface. It does not care if the objects belong to the same hierarchy or not. The only requirement of this function is that objects should implement the draggable interface.
  • In the main method, we have passed Circle object and Person object. They are totally different objects. But still, we can achieve polymorphic behavior because of Interfaces.
  • In the future, if we have new classes that implement this interface can also be passed to this method without changing anything in this function. This makes the code extensible and thus maintainable.

Some Facts - 

  • Interface is defined as a contract as they force the child classes to provide the body of the abstract members.
  • Interfaces cannot contain a state but you can define abstract properties.
  • A class can implement multiple interfaces but can only inherit from one parent class.


That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example