Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Class Functions & Inheritance (Lesson 13 PDF)
Overview about the Lesson
In this lesson, we will cover the following topics:
- How to declare and call class functions in Kotlin
- How to use the
thiskeyword to refer to the current instance of a class - How to override class functions and use the
superkeyword to access the parent class function - How to define and use abstract classes and functions in Kotlin
- How to implement interfaces and multiple inheritance in Kotlin
- How to use the
open,final,sealedanddatamodifiers for classes and functions




The Content of the Lesson
The lesson consists of a PDF file that contains the theory and examples of each topic, as well as exercises and solutions for you to practice. You can download the PDF file from the link at the end of this post. You can also watch the video lecture that accompanies the lesson on our YouTube channel.
Why This Lesson
This lesson is important because it will help you understand how to create and use classes and functions in Kotlin, which are essential for developing any kind of application.
You will also learn how to use inheritance and interfaces to achieve polymorphism, which is the ability of an object to behave differently depending on its type. This will make your code more flexible and extensible.
Kotlin Inheritance, Overriding and Interfaces: Examples
The PDF covers class functions and inheritance. Here are the essentials with runnable code and the output to expect.
Classes are final by default
In Kotlin, a class cannot be inherited unless you mark it open. The same applies to its functions. This “closed by default” design prevents accidental, fragile inheritance.
open class Animal(val name: String) {
open fun sound() = "..."
fun describe() = "$name says ${sound()}"
}
class Dog(name: String) : Animal(name) {
override fun sound() = "Woof"
}
class Cat(name: String) : Animal(name) {
override fun sound() = "Meow"
}
fun main() {
val pets: List<Animal> = listOf(Dog("Rex"), Cat("Misty"))
for (p in pets) println(p.describe())
}
// Output:
// Rex says Woof
// Misty says MeowThis is polymorphism: describe() is written once in the parent, but each subclass supplies its own sound().
Calling the parent implementation with super
open class Employee(val name: String) {
open fun bonus() = 1000
}
class Manager(name: String) : Employee(name) {
override fun bonus() = super.bonus() + 500
}
fun main() {
println(Manager("Hana").bonus()) // 1500
}Abstract classes
abstract class Shape {
abstract fun area(): Double
fun report() = "Area: ${"%.2f".format(area())}"
}
class Circle(val r: Double) : Shape() {
override fun area() = Math.PI * r * r
}
fun main() {
println(Circle(2.0).report()) // Area: 12.57
}An abstract class cannot be created directly; it defines what subclasses must implement.
Interfaces
interface Drivable {
fun drive(): String
fun honk() = "Beep!" // interfaces can have default implementations
}
interface Electric {
fun charge() = "Charging..."
}
class Tesla : Drivable, Electric {
override fun drive() = "Driving silently"
}
fun main() {
val t = Tesla()
println(t.drive()) // Driving silently
println(t.honk()) // Beep!
println(t.charge()) // Charging...
}A class can extend only one class but implement many interfaces.
Abstract class or interface?
| Use… | When |
|---|---|
| An interface | You want to describe a capability that unrelated classes can share (Drivable, Printable) |
| An abstract class | Subclasses share state (properties with values) and common code |
| An open class | The parent is useful on its own but can be specialised |
Practice exercise
Create an open class Account with a function fee() returning 10, and a PremiumAccount that overrides it to return 0.
open class Account {
open fun fee() = 10
}
class PremiumAccount : Account() {
override fun fee() = 0
}
fun main() {
println(Account().fee()) // 10
println(PremiumAccount().fee()) // 0
}Frequently Asked Questions
Why do I get “This type is final, so it cannot be inherited from”?
Because Kotlin classes are final by default. Add open before class in the parent, and open before every function you want to override.
What is the root class of all Kotlin classes?
Any. Every class inherits equals(), hashCode() and toString() from it.
Continue the course
← Previous: Lesson 12: Classes, Objects & Constructors
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance (you are here)
Conclusion
We hope you enjoy this lesson and find it useful for your learning journey. Kotlin is a powerful and expressive language that offers many features and benefits for developers. By learning how to use class functions and inheritance, you will be able to create more complex and sophisticated programs with Kotlin.
Download from Link
You can download the lesson from the following link:














