Kotlin
Java

Polymorphism
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-09-29

Next we'll continue to practice with inheritance. We'll also introduce a new big (literally) idea—polymorphism. Polymorphism may sound scary, but it's not, and we'll work it out together like we always do, using a lot of examples.

Another Puzzle

Let's look at another example of puzzling Kotlin code together:

Explain what is confusing about the code above. printIt takes an Any parameter. So why can we pass a String, a Pet, and even a Course?

Polymorphism

Polymorphism is a big word, and sounds a bit scary. But it's actually quite straightforward. Let's work it out together starting with the Wikipedia definition:

In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types

Go through the Wikipedia definition of polymorphism.

"Is A"

One way to think about polymorphism and Kotlin inheritance is to consider "is a" relationships. For example, every instance of any Kotlin class "is a" Any, because every class is a subclass of Any. Other "is a" relationships depend on inheritance relationships established when classes are declared.

Method Overriding and Polymorphism

One frequent confusion regarding polymorphism has to do with overriding inherited methods. Let's look at how that works:

Up and Down Casting

When we create an instance of a class, we can save it into a variable of any type that it can morph into:

This is referred to as upcasting. Kotlin will automatically upcast an instance to any of its supertypes. Because Dog extends Pet and Pet extends Any, a Dog can be stored in a Dog, Pet, or Object variable.

One thing to note above is that we needed to specify the type of our variables explicitly. If we simply allow Kotlin to perform type inference, each variable above would be of type Dog, since Kotlin will infer the type of the variable to be the type that it is first used to store.

However! The type of the variable determines what we can do with that object. Let's look at how.

Show how upcasting affects the methods that are available.

Don't worry if this seems a bit fuzzy now. We'll return to this topic a few lessons from now when we discuss object references.

Down Casting and Flow Typing

Consider the type hierarchy established below: Given a Pet variable, it might refer to a Dog, a Cat, a Pet or some other kind of pet!

Is there a way that we can tell? Yup! To test if an object is an instance of a particular class, we use the is operator. And, better yet, once we test a type using is Kotlin will automatically allow us to use the methods declared on that class through a process called flow typing. Let's examine how that works:

Describe how to use the is operator to distinguish between different Pet subtypes, and the fact that flow typing makes it instantly availale. Also discuss when with the instance subject and is tests.

Steady There

The last two lessons have been pretty loaded with new ideas and concepts! Exciting, but also enough to make your head spin.

Don't worry. Over the next two lessons we'll slow down and review what we've learned. And then, over the lessons that follow we'll have even more opportunities to integrate this knowledge, but with a small twist. So be patient. This won't all make sense immediately. But it will all make sense eventually.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough