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

In this lesson, you’ll learn:

  • The purpose of an abstract class.
  • The difference between an abstract class and an interface.
  • The requirements for working abstract classes.

Abstract classes are useful when you want to define a base implementation that its subclasses can reuse or have instance variables inherited by its subclasses. The abstract class starts with the abstract keyword, followed by the property’s name or function.

How Does it Work?

Imagine you have a game with the following characters:

Kotlin Abstract Class Declaration

You declare a class as abstract by starting the class with the abstract keyword, followed by the property’s name or function. Here’s how to write the game example from above:

abstract class Adventurer(val name: String, var health: Int) {
    
  abstract fun performSpecialAbility()
  fun move() {
    println("$name is moving.")
  }
}

class Warrior(name: String, health: Int) : Adventurer(name, health) {
  override fun performSpecialAbility() {
    println("$name performs a powerful sword attack!")
  }
}

class Mage(name: String, health: Int) : Adventurer(name, health) {
  override fun performSpecialAbility() {
    println("$name casts a fireball spell!")
  }
}

class Archer(name: String, health: Int) : Adventurer(name, health) {
  override fun performSpecialAbility() {
    println("$name shoots a precise arrow!")
  }
}
fun main() {
  val warrior = Warrior("Conan", 100)
  val mage = Mage("Merlin", 80)
  val archer = Archer("Legolas", 90)

  warrior.move()
  warrior.performSpecialAbility()

  mage.move()
  mage.performSpecialAbility()

  archer.move()
  archer.performSpecialAbility()
}

Kotlin Interface

The Kotlin interface is like a set of instructions that classes must follow. It defines what methods and properties a class must provide.

Kotlin Interface Declaration

You declare an interface using the interface keyword followed by its name and members, its methods and properties. Here’s how you’d write the teacher example as described above:

// Interface representing a Subject curriculum guide
interface Subject {
  // Method declaration for teaching topics
  fun teachTopics()

  // Method declaration for conducting activities
  fun conductActivities()
}
// Concrete class representing a Math teacher
class MathTeacher : Subject {
  override fun teachTopics() {
    println("Teaching math topics.")
  }

  override fun conductActivities() {
    println("Conducting math activities.")
  }
}
// Concrete class representing a Science teacher
class ScienceTeacher : Subject {
  override fun teachTopics() {
    println("Teaching science topics.")
  }

  override fun conductActivities() {
    println("Conducting science activities.")
  }
}
// Concrete class representing a History teacher
class HistoryTeacher : Subject {
  override fun teachTopics() {
    println("Teaching history topics.")
  }

  override fun conductActivities() {
    println("Conducting history activities.")
  }
}

fun main() {
  // Creating instances of different teachers
  val mathTeacher = MathTeacher()
  val scienceTeacher = ScienceTeacher()
  val historyTeacher = HistoryTeacher()

  // Using the teachers to teach their respective subjects
  mathTeacher.teachTopics()
  mathTeacher.conductActivities()

  scienceTeacher.teachTopics()
  scienceTeacher.conductActivities()

  historyTeacher.teachTopics()
  historyTeacher.conductActivities()
}

Differences Between Abstract Class and Interface

You may be wondering when to use an abstract class and when to use an interface. Here are some guidelines that can help you when considering which one to use:

interface FruitBox {
  fun printContents()
}

class AppleBananaBox(private val numOfItems: Int) : FruitBox {
  override fun printContents() {
    println("This fruit box contains $numOfItems apples and bananas.")
  }
}

fun main() {
  val appleBananaBox = AppleBananaBox(10)
  appleBananaBox.printContents()
}

Important Points

Now that you understand what Kotlin abstract classes are and what you can achieve by implementing them in your project, there are a few points that you need to take note of:

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