Implementing Abstract Classes and Interfaces

Tutorial 4 of 5

1. Introduction

In this tutorial, we will learn about abstract classes and interfaces in Kotlin. These concepts are fundamental to object-oriented programming, and understanding them will help us create more flexible and reusable code.

Goal: The goal of this tutorial is to understand and implement abstract classes and interfaces in Kotlin.

Learning Outcomes:
By the end of this tutorial, you will:
- Understand what are abstract classes and interfaces.
- Know how to define and use them.
- Understand the difference between them.

Prerequisites:
- Basic knowledge of Kotlin programming.
- Familiarity with object-oriented programming concepts.

2. Step-by-Step Guide

Abstract Classes

An abstract class is a class that cannot be instantiated and is always meant to be subclassed. They may contain abstract (unimplemented) methods and non-abstract (implemented) methods.

To define an abstract class in Kotlin, we use the keyword abstract.

abstract class AbstractClass {
    abstract fun abstractFunction()
    fun nonAbstractFunction() {
        // Implementation here
    }
}

Interfaces

An interface in Kotlin is similar to an abstract class, with the main difference being that interfaces cannot hold state (they can't have properties that keeps a state). An interface can have properties but these need to be abstract or provide accessor implementations.

To define an interface, we use the interface keyword.

interface MyInterface {
    val property: Int // abstract property
    fun interfaceFunction() // abstract function

    fun implementedFunction() {
        // Implementation here
    }
}

3. Code Examples

Example 1: Abstract class

Let's define an abstract class Animal with an abstract function makeSound().

abstract class Animal {
    abstract fun makeSound()
}

class Dog : Animal() {
    override fun makeSound() {
        println("Woof Woof")
    }
}

fun main() {
    val myDog = Dog()
    myDog.makeSound() // Woof Woof
}

Example 2: Interface

Let's define an interface Flyable with a method fly().

interface Flyable {
    fun fly() 
}

class Bird : Flyable {
    override fun fly() {
        println("Flapping wings...")
    }
}

fun main() {
    val myBird = Bird()
    myBird.fly() // Flapping wings...
}

4. Summary

In this tutorial, we learned what abstract classes and interfaces are in Kotlin. We learned how to define and use them. Abstract classes and interfaces are essential in creating flexible, reusable, and organized code.

To further your understanding, consider exploring how to use interfaces with properties and learning about abstract properties.

5. Practice Exercises

Exercise 1: Create an abstract class Shape with an abstract method calculateArea(). Implement this in two subclasses Circle and Square.

Exercise 2: Create an interface Playable with a method play(). Implement this interface in two classes Instrument and Game.

Solutions:

// Solution for Exercise 1
abstract class Shape {
    abstract fun calculateArea(): Double
}

class Circle(private val radius: Double) : Shape() {
    override fun calculateArea(): Double {
        return Math.PI * Math.pow(radius, 2.0)
    }
}

class Square(private val side: Double) : Shape() {
    override fun calculateArea(): Double {
        return side * side
    }
}
// Solution for Exercise 2
interface Playable {
    fun play()
}

class Instrument : Playable {
    override fun play() {
        println("Playing instrument...")
    }
}

class Game : Playable {
    override fun play() {
        println("Playing game...")
    }
}

For further practice, try to create more classes that implement these interfaces and abstract classes.