Introduction to Types of Inheritance in Kotlin
Session Notes - June 12, 2025
Session Overview
This document provides detailed notes from a session conducted on June 12, 2025, as part of a
one-month-long Kotlin programming course. The session, lasting approximately 1 to 1.5 hours,
focused on the concept of inheritance in Object-Oriented Programming (OOP), specifically ex-
ploring the different types of inheritance supported in Kotlin. The session included theoretical
explanations, diagrams, and practical Kotlin code to demonstrate each type. The code was
executed in a JVM environment (Kotlin version 2.1.21). Below, each type of inheritance is
explained in detail, followed by the code examples and their analysis.
1 Types of Inheritance
Inheritance is a core pillar of OOP, allowing a class (child or subclass) to inherit properties
and methods from another class (parent or superclass). The session covered five types of in-
heritance, with a focus on their structure, purpose, and implementation in Kotlin. Notably,
Kotlin does not support direct multiple inheritance through classes (to avoid complexity like
the diamond problem) but allows it through interfaces.
1.1 Single Inheritance
• Definition: A single class (child) inherits from one parent class.
• Purpose:
– Simplest form of inheritance.
– Promotes code reuse and establishes a direct parent-child relationship.
– Common in hierarchical designs where a subclass extends a single superclass.
• Structure:
Animal
Dog
• Key Points:
1
– In Kotlin, a class must be marked open to allow inheritance (classes are final
by default).
– The child class inherits all accessible members (methods, properties) of the parent.
• Example: Demonstrated with Person and Professor classes (shown later).
1.2 Multilevel Inheritance
• Definition: A child class acts as a parent class for another child class, forming a chain of
inheritance.
• Purpose:
– Allows progressive specialization of classes.
– Enables reuse across multiple levels of hierarchy.
• Structure:
Animal
Dog
Puppy
• Key Points:
– Each class in the chain inherits from the one above it.
– The lowest class (e.g., Puppy) inherits properties and methods from all ancestors
(Dog, Animal).
– Increases complexity, so use cautiously to avoid deep hierarchies.
• Example: Not explicitly coded in the session but explained conceptually.
1.3 Hierarchical Inheritance
• Definition: Multiple child classes inherit from a single parent class.
• Purpose:
– Allows a single parent to share common functionality with multiple subclasses.
– Useful for modeling entities with shared characteristics but distinct behaviors.
• Structure:
Animal
Dog Cat
2
• Key Points:
– Each child class can extend or override the parents behavior independently.
– Promotes modularity and reusability.
• Example: Not coded but discussed as a common pattern (e.g., Dog and Cat sharing
Animal traits).
1.4 Multiple Inheritance (via Interfaces)
• Definition: A class implements multiple interfaces, inheriting their abstract or default
methods.
• Purpose:
– Kotlin prohibits multiple inheritance through classes but allows it through inter-
faces.
– Enables a class to combine behaviors from multiple sources.
– Avoids the diamond problem by using interfaces, which do not carry implementa-
tion conflicts.
• Structure:
Movable Talkable
Robot
• Key Points:
– Interfaces are implemented, not inherited, in Kotlin.
– Interfaces can provide default implementations (as shown in the code).
– A class can implement multiple interfaces, separated by commas.
• Example: Demonstrated with Person implementing Teacher and Writer inter-
faces.
1.5 Hybrid Inheritance
• Definition: A combination of multiple inheritance types, often involving interfaces and
class hierarchies.
• Purpose:
– Combines benefits of single, multilevel, or hierarchical inheritance with multiple
interfaces.
– Allows complex but flexible designs.
• Structure:
3
Person ---- implements ---- Teacher, Writer
Professor
• Key Points:
– Typically involves a class hierarchy (e.g., Person to Professor) and interface
implementation.
– Requires careful design to avoid complexity or ambiguity.
• Example: Shown with Professor inheriting from Person, which implements Teacher
and Writer.
2 Code Examples and Explanations
The following Kotlin code was used to illustrate inheritance concepts, particularly single and
multiple inheritance (via interfaces) and hybrid inheritance.
2.1 Interfaces for Multiple Inheritance
1 interface Teacher {
2 fun teach() {
3 println("Teacher is Teaching")
4 }
5 }
6
7 interface Writer {
8 fun write() {
9 println("I am writing")
10 }
11 }
• Concept: Multiple inheritance via interfaces.
• Explanation:
– Teacher interface defines teach() with a default implementation: prints "Teacher
is Teaching".
– Writer interface defines write() with a default implementation: prints "I am
writing".
– Default implementations allow classes to use methods without overriding, though
overrides are possible.
• Note: Interfaces in Kotlin are lightweight and flexible, supporting multiple inheritance
safely.
4
2.2 Single and Multiple Inheritance
1 open class Person : Teacher, Writer {
2 // override fun teach() {
3 // println("Teacher is Teaching in the person class to the students
")
4 // }
5
6 // override fun write() {
7 // println("Writer is writing in the person class a novel")
8 // }
9 }
10
11 class Professor : Person() {
12 fun conductResearch() {
13 println("I am reasearching")
14 }
15 }
• Concepts: Single inheritance (Professor from Person) and multiple inheritance
(Person implementing Teacher and Writer).
• Explanation:
– Person: An open class implementing Teacher and Writer interfaces.
– Inherits default implementations of teach() and write() from interfaces.
– Commented-out overrides: Would customize teach() and write() for Person
but were not active in the session.
– Professor: Inherits from Person (single inheritance), gaining access to teach()
and write() via Persons implementation of interfaces.
– conductResearch(): A Professor-specific method, prints "I am reasearch-
ing" (typo: should be "researching").
• Note: The commented-out overrides suggest flexibility to customize interface methods
but were not used, so default implementations apply.
2.3 Main Function
1 fun main() {
2 val prof = Professor()
3 prof.teach()
4 prof.write()
5 prof.conductResearch()
6 }
• Explanation:
– val prof = Professor(): Creates an instance of Professor.
– prof.teach(): Calls teach() from Teacher interface via Person, outputs
"Teacher is Teaching".
5
– prof.write(): Calls write() from Writer interface via Person, outputs
"I am writing".
– prof.conductResearch(): Calls Professors method, outputs "I am reasearch-
ing".
• Output:
Teacher is Teaching
I am writing
I am reasearching
• Note: Demonstrates single inheritance (Professor from Person) and multiple in-
heritance (Person implementing Teacher and Writer).
3 Key Takeaways and Notes
• Single Inheritance: Professor inherits from Person, reusing and extending func-
tionality.
• Multilevel Inheritance: Not coded but explained as a chain (e.g., Animal Dog Puppy).
• Hierarchical Inheritance: Not coded but discussed as multiple subclasses from one
parent (e.g., Dog and Cat from Animal).
• Multiple Inheritance: Achieved via interfaces (Person implements Teacher and
Writer).
• Hybrid Inheritance: Combines class hierarchy (Person Professor) with interface
implementation.
• Code Notes:
– Typo: "reasearching" in conductResearch() should be "researching".
– Commented-out overrides in Person suggest customization options but were not
used, relying on default interface implementations.
– Kotlins open keyword is required for class inheritance; interfaces are implemented,
not inherited.
• Session Focus: This 1 to 1.5-hour session provided a comprehensive introduction to
inheritance types in Kotlin, blending theory, diagrams, and practical code to illustrate
single, multilevel, hierarchical, multiple (via interfaces), and hybrid inheritance.