Skip to main content
infervour.com

Back to all posts

How to Override Method With Inherited Class In Kotlin?

Published on
4 min read
How to Override Method With Inherited Class In Kotlin? image

Best Kotlin Programming Books to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

In Kotlin, you can override a method from a superclass in a subclass by using the override keyword before the method declaration in the subclass. This allows you to provide a different implementation for the method in the subclass, while still maintaining the same method signature as the superclass.

When you override a method in a subclass, you can call the superclass implementation using the super keyword. This allows you to add additional functionality to the method in the subclass without completely replacing the superclass implementation.

To override a method in a subclass in Kotlin, the method in the superclass must be declared with the open keyword. This indicates that the method can be overridden in subclasses. If the method in the superclass is not marked as open, you will not be able to override it in a subclass.

When working with inheritance in Kotlin, it is important to carefully consider which methods should be open for overriding in subclasses, and which methods should be final and not allowed to be overridden. This will help ensure that your code is clear, maintainable, and easy to understand.

How can you achieve method overriding in Kotlin?

To achieve method overriding in Kotlin, you need to follow these steps:

  1. Create a base class with a function that you want to be overridden in a subclass.

open class BaseClass { open fun someFunction() { println("This is a function in the base class") } }

  1. Create a subclass that inherits from the base class and overrides the function.

class SubClass : BaseClass() { override fun someFunction() { println("This is a function in the subclass") } }

  1. Now you can create an object of the subclass and call the overridden function.

fun main() { val subClass = SubClass() subClass.someFunction() }

This will output:

This is a function in the subclass

What is the purpose of method overriding in Kotlin?

Method overriding in Kotlin allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This allows for customizing behavior of a method in the subclass without changing the method signature in the superclass. This is useful for achieving polymorphism and allowing different classes to have different implementations of the same method based on their specific needs.

What is the behavior of Kotlin when a method is overridden by multiple subclasses?

In Kotlin, when a method is overridden by multiple subclasses, the behavior will depend on which subclass is being referenced. If a method is overridden by multiple subclasses and the method is being called on an object of a particular subclass, the overridden method of that specific subclass will be executed. This is known as dynamic method dispatch or runtime polymorphism.

For example, consider a class Animal with a method makeSound() that is overridden by subclasses Dog and Cat. If we have an object of type Animal and call the makeSound() method on it, the actual behavior will depend on whether the object is actually an instance of Dog or Cat.

open class Animal { open fun makeSound() { println("Generic Animal sound") } }

class Dog : Animal() { override fun makeSound() { println("Woof") } }

class Cat : Animal() { override fun makeSound() { println("Meow") } }

fun main() { val animal1: Animal = Dog() val animal2: Animal = Cat()

animal1.makeSound() // Output: Woof
animal2.makeSound() // Output: Meow

}

In the above example, the makeSound() method is overridden by both Dog and Cat subclasses. When the makeSound() method is called on animal1 and animal2, the overridden method of the specific subclass will be executed, resulting in "Woof" for Dog and "Meow" for Cat.