Blog Infos
Author
Published
Topics
, , , ,
Published

1. What is inheritance?

It’s a way for classes to ‘inherit’ all properties from another class. We usually say that the class we inherit is called a superclass, and the class that inherits properties is called a subclass.

In Kotlin, there’s Single Inheritance only, meaning there’ll always be a single subclass and a single superclass. In Kotlin, we can use it in two ways:

  1. With open class
// With open classes
open class Cat {
    fun meow() {
        println("meow")
    }
}

class SpecificCat : Cat() {
    fun specificFunction() {
        println("specific")
    }
}

fun main() {
    val cat = Cat()
    cat.meow() // meow
    val specificCat = SpecificCat()
    specificCat.meow() // meow
    specificCat.specificFunction() // specific
}

2. With abstract class

abstract class Cat {
    fun meow() {
        println("meow")
    }

    abstract fun specificFunction()
}

class SpecificCat : Cat() {
    override fun specificFunction() {
        println("specific")
    }
}

fun main() {
    // Creating Cat throws compilation error
    // we can't create abstract classes
    // val cat = Cat()
    val specificCat = SpecificCat()
    specificCat.meow() // meow
    specificCat.specificFunction() // specific
}

Not that we would try to make SpecificCat inherit from two classes, it’s impossible:

open class WalkingCat {
    fun walk() {
        println("walk")
    }
}

// Compilation error: Only one class may appear in a supertype list
class SpecificCat : Cat(), WalkingCat() {
    override fun specificFunction() {
        println("specific")
    }
}

These are the main limitations:

  • We can only inherit from a single class.
  • We take everything from that class, even things we don’t need.
  • It’s harder to read, especially when working with multiple levels of inheritance. SpecificCat could inherit from Cat and then SpecificWalkingCat could inherit from SpecificCat while using methods defined in Cat class. It’s hard to navigate and understand that type of code.
2. What is Composition?

It’s a principle that classes should have ‘has a’ relationships. This way, we can divide and ‘compose’ the properties we class should have. It’s done through Interface s and classes that implement them.

We could refactor the Inheritance example to use composition:

interface Mouth {
    fun speak()
}

class CatMouth : Mouth {
    override fun speak() {
        println("meow")
    }
}

class SpecificCat {
    private val mouth: Mouth = CatMouth()
    
    fun meow() {
        mouth.speak()
    }
    
    fun specificFunction() {
        println("specific")
    }
}

fun main() {
    val specificCat = SpecificCat()
    specificCat.meow() // meow
    specificCat.specificFunction() // specific
}

Or, even better, by using Kotlin delegates, we’re able to add all the Mouth functions to SpecificCat :

Job Offers

Job Offers


    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

No results found.

Jobs

class SpecificCat : Mouth by CatMouth() {
    fun specificFunction() {
        println("specific")
    }
}

fun main() {
    val specificCat = SpecificCat()
    // Now we use 'speak' because meow was removed
    specificCat.speak() // meow
    specificCat.specificFunction() // specific
}

The main benefits of using composition are:

  • Reusability — you can reuse the implementation of an interface in multiple classes.
  • ‘has a’ relationship — you can have multiple properties and compose code more readably.
  • You control what is visible to the outside world and what is not!
  • You can leverage Kotlin Delegates to add all properties from the implementing class without the drawbacks of inheritance.

If you’ve learned something new please clap and follow me for more!

This article is previously published on Proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu