Kotlin Tutorial 11 — Inheritance
In this tutorial OOP inheritance will be covered with classes and interfaces. Kotlin provides three types of inheritance: classic (via classes), composition (via interfaces), and delegation which isn’t covered in this tutorial series. Unlike Python Kotlin only does single inheritance but can partially do multiple inheritance by composition. Before going further in this tutorial you need to create a new Kotlin project in IntelliJ called kotlin_tutorial11 in ~/kotlin_projects.
Classic Inheritance
The original way to do inheritance by having a class inherit from another class (only one). Create a new package in src called org.example.inheritance.classicinheritance. In the package create a new Kotlin file called class_inheritance with the following contents:
Run the class_inheritance.kt file. By default a Kotlin class is closed (sealed) unlike Python which means a class cannot be inherited from unless it is opened (using the open keyword). Note that a class (sub class) inheriting from another class must initialise the constructor of the super class.
Multiple constructors can exist on a class. Create a new Kotlin file in org.example.inheritance.classicinheritance called class_inheritance2 with the following contents:
Run the class_inheritance2.kt file. Secondary constructors in a class must call the primary constructor before doing any object initialisation. See above how the this (refers to the object instance of the class, equivalent to Python’s self) keyword is used to avoid namespace conflicts, where the secondary constructor parameter materials shares the same name as the property in the class.
Composition
Instead of doing inheritance the classical way it is done through one or more interfaces. An interface is similar to a class except it doesn’t create objects and cannot have initialised properties. With composition a class or interface can inherit from one or more interfaces (there is no limit with composition). Create a new package called org.example.inheritance.composition. In the package create a new Kotlin file called composition with the following contents:
Run the composition.kt file. As you can see above interfaces can have functions with a body. If a class inherits from an interface that has a function without a body (abstract function) then the class must implement the function. Also if a class inherits from a interface that has a property without a getter (get function) then the class must implement the property. Create new a package in src called org.example.inheritance.abstractcomposition. In the package create a new Kotlin file called abstract_composition with the following contents:
Run the abstract_composition.kt file.
Conclusion
Having been introduced to interfaces and gaining an understanding of how inheritance works in Kotlin there are some final OOP concepts to cover. Next tutorial covers the last two OOP concepts in Kotlin Tutorial 12 — Encapsulation And Polymorphism.
TOC
- Kotlin Tutorial 1 — Introduction
- Kotlin Tutorial 2 — Basic Data Types
- Kotlin Tutorial 3 — Basic Operators
- Kotlin Tutorial 4 — Handling Input And Output
- Kotlin Tutorial 5 — Basic Collections
- Kotlin Tutorial 6 — Control Flow
- Kotlin Tutorial 7 — Functions
- Kotlin Tutorial 8 — Modularity
- Kotlin Tutorial 9 — Using An IDE
- Kotlin Tutorial 10 — Classes
- Kotlin Tutorial 11 — Inheritance
- Kotlin Tutorial 12 — Encapsulation And Polymorphism
- Kotlin Tutorial 13 — Generics