Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Introducing Inheritance

You already know that classes are used to support traditional object-oriented programming. Class concepts include inheritance, overriding, polymorphism and composition, which makes them suited for this purpose. Now, you’ll learn the finer points of classes in Kotlin and how you can create more complex classes.

class Food(
  val name: String,
  var price: String,
  var origin: String)

class Fruit(
  val name: String,
  var price: String,
  var origin: String,
  val stone: Boolean,
) {
  fun hasStone():Boolean {
    return stone
  }
}
// Fruit is a Food
open class Food(
  val name: String,
  var price: String,
  var origin: String) {

  fun label(): String {
    return "$name of $origin. Price: $price"
  }
}
class Fruit(
  name: String,
  price: String,
  origin: String,
  val stone: Boolean = false
): Food(name, price, origin)
 {
  fun hasStone(): Boolean {
    return stone
  }
}
fun main() {
  val tomato = Food("Tomato", "1.0", "US")
  val tomato2 = Fruit("Tomato", "1.0", "US")

  println(tomato.label()) //Tomato of US. Price: 1.0
  println(tomato2.label()) //Tomato of US. Price: 1.0
}
println(tomato.hasStone()) // Error: Unresolved reference: hasStone
  println(tomato2.hasStone()) // false

Polymorphism

Have you ever noticed how a chameleon can adapt its appearance to blend into its surroundings? Polymorphism in Kotlin is like a chameleon for your code! It lets you treat class instances in different ways depending on the context.

// Polymorphism
open class Food(
  val name: String,
  var price: String,
  var origin: String) {

  fun label(): String {
    return "$name of $origin. Price: $price"
  }
}
class Fruit(
  name: String,
  price: String,
  origin: String,
  val stone: Boolean = false
): Food(name, price, origin) {
  fun hasStone(): Boolean {
    return stone
  }
}
class Veg(
  name: String,
  price: String,
  origin: String,
  val rooted: Boolean = false
): Food(name, price, origin) {
  fun isRooted(): Boolean {
    return rooted
  }
}
fun foodLabel(food: Food) : String {
  return "Label: ${food.label()}"
}
fun main() {
  val tomato = Fruit("Tomato", "1.0", "US")
  val carrot = Veg("The Carrot", "2.0", "Canada", true)

  println(foodLabel(tomato)) // Label: Tomato of US. Price: 1.0
  println(foodLabel(carrot)) // Label: The Carrot of Canada. Price: 2.0
}

Super

In Kotlin, when you override a method in a subclass, you can leverage the super keyword to interact with the superclass’s version of the method. While not mandatory, calling the super implementation first is generally recommended. This ensures two key benefits:

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo