Kotlin Overriding Methods

beginner
14 min

Kotlin Overriding Methods

Welcome to your journey into the world of Kotlin! Today, we'll be diving into the fascinating concept of Overriding Methods. By the end of this tutorial, you'll have a solid understanding of how to override methods in Kotlin, making you one step closer to becoming a proficient Kotlin developer. 🎯

Why Override Methods?

Overriding methods is a powerful feature in Kotlin that allows you to customize inherited methods from a parent class in a child class. This can be extremely useful when you want to modify the behavior of an existing method to suit your specific needs. 📝

Let's get started by understanding the basics.

Understanding Methods

Before we dive into method overriding, let's first familiarize ourselves with methods in Kotlin. Methods are functions defined within a class that perform specific tasks. They can accept parameters and return values.

kotlin
class MyClass { fun myMethod() { // Method code } }

In the above example, myMethod is a method defined in the MyClass class.

Super and Overriding Methods

Now, let's introduce super and overriding methods. The super keyword is used to call a method in the superclass (parent class) from the subclass (child class).

When you override a method in a subclass, you're effectively replacing the method in the superclass with a new implementation in the subclass.

kotlin
class ParentClass { fun myMethod() { println("This is the parent's method.") } } class ChildClass : ParentClass() { override fun myMethod() { println("This is the child's method.") super.myMethod() // Calling the parent's method } }

In the above example, we have a ParentClass with a myMethod and a ChildClass that extends ParentClass. We override the myMethod in the ChildClass and call the parent's method using super.myMethod().

Overriding Methods Rules

  1. Overriding methods must have the same name, return type, and parameters (excluding varargs) as the method in the superclass.
  2. The override keyword must be used before the method declaration.
  3. Overriding a method in a subclass hides the method in the superclass.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of using the `super` keyword in Kotlin?

Method Overriding Examples

Here are two examples demonstrating method overriding in Kotlin.

Example 1 - Overriding Equals Method

kotlin
class Person(val name: String, val age: Int) { override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || this::class != other::class) return false val person = other as Person return name == person.name && age == person.age } } val person1 = Person("John", 25) val person2 = Person("John", 25) val person3 = Person("Alice", 20) println(person1 == person2) // true println(person1 == person3) // false

In this example, we've overridden the equals method in the Person class to compare the names and ages of two Person objects.

Example 2 - Overriding toString Method

kotlin
class Rectangle(val width: Int, val height: Int) { override fun toString(): String { return "Rectangle(width=${width}, height=${height})" } } val rectangle = Rectangle(5, 10) println(rectangle) // Rectangle(width=5, height=10)

In this example, we've overridden the toString method in the Rectangle class to provide a readable representation of a Rectangle object.

And there you have it! You've now learned the basics of method overriding in Kotlin. As you continue to explore Kotlin, you'll find that overriding methods is an invaluable skill for customizing and modifying inherited behavior in your code. Happy coding! 💡📝✅