Better Programming

Advice for programmers.

Follow publication

Object-Oriented Programming in Kotlin

As explained with code examples and illustrations

Ankush Kumar Singh
Better Programming
Published in
4 min readJan 30, 2022
Photo by Ayla Verschueren on Unsplash

Kotlin is one of the popular programming languages used for Android and backend development. Popular tech companies like Meta, Google, and Apple have included Kotlin in their tech stack.

In this blog, we will discuss object-oriented programming topics of Kotlin like classes, abstract classes, interface, etc.

Kotlin supports basic types like int, long, string etc.

However, classes let the user define their own types and properties. Like other programming languages a class in Kotlin consist of functions and properties (variables). Let us create a class Dog.

Illustration by author

UML diagram below describes a class Dog which has properties name, weight, height and avgLifeExpectancy.

It also has function which defines actions that class Dog can perform, like speak, walk and eat.

Following is the code snippet of the class definition:

Sample output of the program:

woof!
nom nom!
I love walking with my hooman. And occasional zoomies!
nora has following properties,
Weight is 10
Height is 22
Average life expectancy is 10

In the code above, myDog is an object of class Dog. This object can be used to access functions and variables defined inside the class. It is interesting to note that variables are defined using keywords var and val.

Variables defined with keyword var are mutable while variables defined with keyword val are immutable.

In the example above, like other programming languages class properties can be accessed like classObject.property.

It has a caveat that negative value can be assigned to properties like weight and height. To avoid this we can use setters to sanitize input before assigning to class property.

Kotlin defines init block which gets executed when the class is initialized. This is useful for the cases when we have any pre-requisite to the class’s main functionality.

Following is the code snippet to illustrate this:

Sample output of the program is:

This is init block. Notice where it's getting executed!
woof!
nom nom!
I love walking with my hooman. And occasional zoomies!
Nora has following properties,
Weight is 10
Height is 22
Average life expectancy is 10
Weight = 10
Changing weight to 30
New weight is 30
Changing weight to -30
Weight after assigning negative value is 30

It’s interesting to note that weight property was not updated with the negative value when we tried to do so. It is because of the check in the setter for weight property.

I hope we are getting comfortable with classes in Kotlin. Let’s expand the scope to understand inheritance. To do so we will have classes Animal, Cat and Dog. Animal class is an abstract class which will be inherited by classes Cat and Dog. Following the UML diagram for this:

Following is the code snippet for this:

Sample code output:

woof!
nom nom!
I love walking with my hooman. And occasional zoomies!
meow!
nom nom!
Hooman, you better not leash me!

In the code example above, class Animal is the abstract class. Functions speak and walk are overridden by class Dog and class Cat. It is done so by using keyword override.

Image by author

Abstract classes make code more extendable but it has a limitation that child classes can have only one parent class. Interfaces can be used to support multiple inheritance which means a class can implement multiple interfaces. Following is the UML diagram of a sample interface:

As the UML diagram describes, class Dog implements interfaces Animal and LivingThings. Following is the code snippet for this:

Sample output:

woof!
nom nom!
I love walking with my hooman. And occasional zoomies!
I might be an Airbender! May be. Just May be.
Growing with the time.

As we can see in the code above, variable alive and function speak, walk and breath are implemented in the class Dog using keyword override.

Interfaces and abstract classes sound very similar but with a slight difference.

The interface provides the blueprint of the class, in other words, it defines a contract about the capabilities that it will deliver and the class implementing it is responsible for delivering those promises.

However, an abstract class is incomplete implementation which gets extended by the child class.

I hope you had a great time learning Kotlin object-oriented programming with me. Thank you for reading.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (1)

Write a response