Kotlin Tutorial 12 — Encapsulation And Polymorphism
All remaining OOP concepts (encapsulation and polymorphism) will be covered in this tutorial which ties up OOP in Kotlin. Create a new Kotlin project in IntelliJ called kotlin_tutorial12 in ~/kotlin_projects.
Encapsulation
OOP encapsulation in Kotlin unlike Python is enforced and has some fine grained levels (scope modifiers/keywords) which are the following:
- private — With a top level element it is not accessible outside the Kotlin file it is defined in, elements defined in a class/interface (eg properties, functions) are only accessible in the same place where they are defined
- public — Elements can be accessed anywhere
- protected — Same as private except sub classes can access class/interface elements (includes member properties and functions unless they are marked private), this encapsulation level isn’t supported on top level elements
- internal — Anything in the module can access elements defined in the same module
A module in Kotlin is a set of Kotlin files which could be represented by a JAR file. By default all defined elements (not using a scope modifier) are public in scope just like Python. Create a package in src called org.example.enpoly. In the package create a new Kotlin file called AnimalBase with the following contents:
Polymorphism
Kotlin requires the use of the override keyword unlike Python when an element is being overridden in a class/interface, and checks are made to see if the element can be overridden at compile time. Any class that can be inherited from must be open and any of its defined elements that can be overridden must also be open. Create a new package in src called org.example.enpoly.dog. In the package create a new Kotlin file called Dog with the following contents:
See in the sample above that the stats function doesn’t have the open keyword which means that the function cannot be overridden. Create a new Kotlin file in the same package called SmallDog with the following contents:
Create a new Kotlin file in the same package called LargeDog with the following contents:
Now for some classes to cover two dog breeds. Create a new Kotlin file in the same package called Chiwawa with the following contents:
Create a new Kotlin file in the same package called GreatDane with the following contents:
Finally a main Kotlin file needs to be created to tie everything together. In the org.example.enpoly package create a new Kotlin file called main with the following contents:
Run the main.kt file.
Conclusion
All key OOP concepts have been covered. Next tutorial will be the final one in the tutorial series covering one last topic in Kotlin Tutorial 13 — Generics.
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