In this tutorial, we will delve into the concepts of inheritance and polymorphism in Kotlin. The goal is to understand how to use inheritance to derive new classes from existing ones and how to use polymorphism to create flexible code structures.
By the end of this tutorial, you will learn:
- What inheritance and polymorphism are
- How to use inheritance to create derived classes
- How to apply polymorphism in Kotlin
Prerequisites:
- Basic knowledge of Kotlin syntax
- Understanding of classes and objects in Kotlin
In Kotlin, inheritance is a mechanism where you can derive a class from another class for a hierarchy of classes that share a set of attributes and methods. The class which inherits the properties of another class is called the derived class, and the class whose properties are inherited is called the base class.
Polymorphism is a concept by which we can perform a single action in different ways. So in Kotlin, you can define a method in a class and have a same named method in its child class. This is known as method overriding, or runtime polymorphism.
open
keyword to allow a class to be inheritable.override
keyword when overriding a method in a derived class.Example 1: Basic Class Inheritance in Kotlin
// A 'open' class animal
open class Animal {
open fun sound() {
println("The animal makes a sound")
}
}
// A class 'Dog' derived from 'Animal' class
class Dog : Animal() {
override fun sound() {
println("The dog barks")
}
}
fun main(args: Array<String>) {
val myDog: Animal = Dog()
myDog.sound()
}
In this example, Dog
is a derived class that inherits from the Animal
base class. The sound
method in Dog
overrides the sound
method in Animal
.
Expected output:
The dog barks
Example 2: Polymorphism in Kotlin
open class Animal {
open fun sound() {
println("The animal makes a sound")
}
}
class Dog : Animal() {
override fun sound() {
println("The dog barks")
}
}
class Cat : Animal() {
override fun sound() {
println("The cat meows")
}
}
fun main(args: Array<String>) {
val myDog: Animal = Dog()
val myCat: Animal = Cat()
myDog.sound()
myCat.sound()
}
In this example, both Dog
and Cat
classes override the sound
method in their own way, exhibiting polymorphism.
Expected output:
The dog barks
The cat meows
In this tutorial, we covered the concepts of inheritance and polymorphism in Kotlin. We learned how to use inheritance to derive new classes and how to use polymorphism to create flexible code structures.
Next, you can explore other object-oriented concepts in Kotlin, like abstraction and encapsulation.
Additional resources:
- Kotlin Documentation
- Kotlin for Java Developers (Coursera)
Exercise 1: Create a base class Shape
with a method area()
. Derive two classes Rectangle
and Circle
from Shape
and override the area()
method.
Exercise 2: Implement polymorphism by creating an array of Shape
objects and calling the area()
method.
Solutions:
// Solution for Exercise 1
open class Shape {
open fun area() {
println("Area of shape")
}
}
class Rectangle(var width: Int, var height: Int) : Shape() {
override fun area() {
println("Area of rectangle is ${width * height}")
}
}
class Circle(var radius: Int) : Shape() {
override fun area() {
println("Area of circle is ${3.14 * radius * radius}")
}
}
// Solution for Exercise 2
fun main(args: Array<String>) {
val myShapes: Array<Shape> = arrayOf(Rectangle(4,5), Circle(3))
for(shape in myShapes) {
shape.area()
}
}
In the first exercise, Rectangle
and Circle
classes are derived from Shape
and override the area()
method. In the second exercise, an array of Shape
objects is created and area()
method is called for each object, demonstrating polymorphism.