Lesson 7 - Inheritance and polymorphism in Kotlin

Kotlin OOP Inheritance and polymorphism in Kotlin

In the previous lesson, Arena with warriors in Kotlin, we made an arena that simulates two warriors in battle. In today's Kotlin tutorial, we're going to expand our knowledge of object-oriented programming. In the introduction of this course, we mentioned that the OOP is based on three fundamental concepts: encapsulation, inheritance and polymorphism. We're already familiar with the encapsulation and have used the private access modifier. Today, we're going to take a look at the two remaining concepts.

Inheritance

Inheritance is one of the basic features of OOP and is used to create new data structures based on old ones. Let's look at a simple example:

Imagine that we were hired to create a database that will keep track of animals at a zoo :) There will be two types of users in our system: users and administrators. Users will be common zookeepers who will be able to edit information about the animals, e.g. their weight or wingspan. The administrator will be able to modify animal data as well and add or delete animals from the database. Administrators will have an extra field, a phone number so they can be contacted in case of system failure. It'd be pointless and confusing to fully define both classes since they have many fields and methods in common. Both the user and the administrator will have names, ages and be able to log in and log out. Today will mostly be about going over the concepts, we'll program a fully functioning example in the next lesson (the following code is just a draft):

class User {
        private val name: String = ""
        private val password: String = ""
        private val age: Int = 0

        fun logIn(password: String): Boolean {
                // ...
        }

        fun logOut(): Boolean {
                // ...
        }

        fun setWeight() {
                // ...
        }

        // ...

}

We've just made a general outline of the class, but I'm sure you get the gist of it. Without knowing what inheritance is, we would define our Administrator class like this:

class Admin {
        private val name: String
        private val password: String
        private val age: String
        private val telNumber: String

        fun logIn(password: String): Boolean {
                // ...
        }

        fun logOut(): Boolean {
                // ...
        }

        fun setWeight(animal: Animal) {
                // ...
        }

        fun addAnimal(animal: Animal) {
            // ...
        }

        fun removeAnimal(animal: Animal) {
                // ...
        }

        // ...

}

We can see that our code is very redundant in this class. All further changes would have to take a place in both classes, it'd make the code very hard to maintain. Now, we'll declare the administrator class using inheritance. First let's tell Kotlin that the User class can be inherited. Next, we'll define the Administrator class in a way so it can be inherited from the User class. We don't have to declare fields and methods again in Administrator, Kotlin will add them automatically.

Classes in Kotlin are sealed by default, so they can't be inherited. For those of you who came from Java: we can compare the situation to the final class modifier.

We'll open the User class making it inheritable. We can do so using the open keyword:

open class User {
        // ... the rest of the methods and fields...
}

Next, we'll define the Administrator class, inheriting from the User class:

class Administrator: User {
        private val telNumber: String = ""

        fun addAnimal(animal: Animal) {
                // ...
        }

        fun removeAnimal(animal: Animal) {
                // ...
        }

        // ...
}

We can see that we've used the : operator for inheritance.

In the example above, the private fields won't be accessible to the descendant. Only the fields and methods with the public modifier will. private fields and methods are understood as a special logic that belongs to a certain class. Meaning that, they are hidden from descendants - even a descendant is actually using this logic, it's unable to modify it. To achieve the desired result, we'll use a new access modifier - protected which works the same way as private but allows us to inherit these fields or methods.

The beginning of the User class would look like the following:

/---class java class User {
protected val name: String = ""
protected val password: String = ""
protected val age: Int = 0
// ... \--

Now, if we created user and administrator instances, they'd both have name fields and logIn() methods. Kotlin will inherit the User class and redeclare all its fields automatically.

The advantages of using inheritance are clear. We don't have to copy the same fields to two classes. All we have to do is declare new ones. The rest will be inherited. The benefits are tremendous, we can extend existing components of new methods and reuse them. We don't have to write lots of redundant code, and most importantly - when we change a single field in the parent class, this change is then inherited everywhere automatically. We don't need to change something manually in 20 classes, which could potentially cause errors. We're humans and we'll always make mistakes, we have to choose the right programming techniques to make as less mistakes as possible.

The parent class is sometimes called the ancestor, our User class, and the class that inherits from it, a descendant, our Administrator class. Descendants may add new methods or override the existing methods of the parent class (see example below). Sometimes, you may encounter the terms superclass and subclass which essentially mean the same thing.

Another way to design the object model would be to create a User parent class whose sole purpose would be inheritance. The User would be then inherited by Attendant and Admin classes. This technique would be useful if we had lots of user types. In that case, we're talking about a class hierarchy, which we'll get into further along in this online course. Our example was simple so two classes were enough. There are also design patterns that are established and approved object models used for common situations. All of that stuff is advanced and interesting, and I promise we'll get to them later as well. In object modeling, we draw inheritance as an empty arrow pointing to the parent class. In our case, the graphical notation would look like this:

Object inheritance – graphical notation

Inheritance and the data type

The biggest advantage to using inheritance is that if we create a variable of the parent data type, we can store the descendants in it as well. We are able to do this because a descendant always includes everything that the parent class does, therefore, it meets the "requirements" of the data type (more accurately it provides the necessary interface). What all descendants are is the parent class with some extra stuff. That way, we are able to create an array of the User data type and store there both users and administrators. Here is an example assigning a descendant to a variable of the ancestor type and vice-versa:

val u = User("John Newman", 33)
val a = Administrator("Jack White", 25)
// now we assign the administrator to the variable of the User type:
u = a
// It's fine since the User class is its parent class
// If we try in conversely, we'll cause an error:
a = u

There are many constructs in Kotlin, that allow us to work with data types of inherited instances. We'll go over them in detail during the course. For now, we'll demonstrate how we can verify the data type of an instance in a variable:

val u = Administrator("Jack White", 25)

if (u is Administrator) {
        println("The user given has been identified as an administrator")
} else {
        println("The user given is not an administrator")
}

We can ask whether an object is of a given type using the is operator. The code above checks whether there is a user or its descendant, administrator, in the u variable.

Languages can support either single or "multiple inheritance". With the single inheritance, a class can inherit only from one other class. With the "multiple inheritance", a class can inherit from several classes at the same time. Multiple inheritance never became very popular. We'll discuss why and show you how to work around it later. Kotlin only supports the single inheritance. You can encounter multiple inheritance in the C++ language.

Polymorphism

Don't be scared of the obscure name of this technique, it's actually very simple. Polymorphism allows us to use a unified interface to work with objects of different types. Imagine that we have, for example, many objects representing geometric shapes (circles, squares, triangles, and so on). It'd certainly be helpful if we could communicate with them in some unified way even though they're different. We can create a GeometricShape class containing a color field and a render() method. All the geometric shapes would then inherit the interface from this class. Now you may be thinking, "But the circle and square object would render differently!." Well, polymorphism allows us to override the render() method in** every subclass so it will do what we want. The interface will be unified and we won't have to think about which method to call to render different objects.

Polymorphism is often explained using animals. All having a speak() method in their interface, but each animal performs it differently.

Polymorphism

The essence of polymorphism is a method or methods, that all the descendants have defined with the same heads, but with different method bodies. We'll use polymorphism along with inheritance in the next lesson, , on our warriors in the arena. We'll add a mage who will inherit warrior's fields and behavior and override the warrior's attacking method to use mana. We won't be able to tell that he's not a pure warrior from the outside since he will have the same interface. It'll be fun :)


 

 

Article has been written for you by Samuel Kodytek
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
Thumbnail
All articles in this section
Object-oriented programming in Kotlin
Activities (2)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!