Kotlin Override Method of Super Class

In Kotlin, a method of a super class can be overridden in a child class when the method is marked with the open keyword in the super class and the child class method uses the override keyword. Method overriding is useful when a base class provides a common behavior, but a subclass has to provide a more specific implementation of the same behavior.

Kotlin is strict about inheritance. Classes and functions are final by default. Therefore, both the class and the function must be explicitly made open before a subclass can override the function.

Kotlin Method Override Rules

Before overriding a method in Kotlin, keep these rules in mind.

  • The parent class must be declared with open.
  • The parent method must be declared with open.
  • The child class must use the override keyword.
  • The overriding function must have a compatible signature.
  • An overridden function is open by default unless it is marked as final override.

Syntax – Kotlin Inheritance

The syntax to override method of a Parent class in Child class is

</>
Copy
open class SuperClass(primary_constructor){
	open fun behaviour_1(){
	// common behaviour
	}
}

class ChildClass(primary_constructor): SuperClass(primary_constructor_initialization){
	fun behaviour_1(){
	// custom behaviour overriding common behaviour
	}
}

Note: The method to be overridden has to be open.

In actual Kotlin code, the overriding method in the child class should also include the override keyword. A corrected minimal syntax is shown below.

</>
Copy
open class SuperClass {
    open fun behaviour() {
        // common behaviour
    }
}

class ChildClass : SuperClass() {
    override fun behaviour() {
        // custom behaviour
    }
}

Example 1 – Override Method of Super Class in Kotlin

In the following example we shall define a super/parent class named Person, and child class named Student.

example.kt

</>
Copy
fun main(args: Array<String>) {
    var student_1 = Student("Arjun")

    println("\n\nAbout "+student_1.name+"\n---------------")
    student_1.doAll()
}

/**
 * Person is a Super Class
 */
open class Person(var role: String = "Person", var name: String = "X") {
    fun eat(){
        println(name + " is eating.")
    }
    open fun sleep(){
        println(name + " is sleeping.")
    }
}

/**
 * Student class inherits Person class
 */
class Student(name: String): Person("Student", name) {
    // activity function belongs to Student only
    fun activity(){
        println("$name is a $role. $name is studying in school.")
    }

    // overrides sleep function of Person class
    override fun sleep(){
        println("$name is a $role. $name goes to bed early.")
    }

    fun doAll(){
        eat()
        sleep()
        activity()
    }
}

When the above program is run, you will get the following output.

Output

About Arjun
---------------
Arjun is eating.
Arjun is a Student. Arjun goes to bed early.
Arjun is a Student. Arjun is studying in school.

In this program, Person is declared as an open class. Its sleep() function is also declared as open, so the Student class can override it. The eat() function is not open, so it is inherited as-is and cannot be overridden by Student.

Calling the Super Class Method from an Overridden Kotlin Method

Sometimes the child class should extend the parent behavior instead of replacing it completely. In that case, call the parent implementation using super.methodName() inside the overridden method.

</>
Copy
fun main() {
    val student = CollegeStudent("Meera")
    student.introduce()
}

open class Learner(val name: String) {
    open fun introduce() {
        println("My name is $name.")
    }
}

class CollegeStudent(name: String) : Learner(name) {
    override fun introduce() {
        super.introduce()
        println("I am a college student.")
    }
}

Output

My name is Meera.
I am a college student.

Here, CollegeStudent first executes the implementation from Learner and then adds its own statement. This approach is useful when the child class should keep the base logic and add only the extra behavior.

Preventing Further Override with final override in Kotlin

When a method is overridden in Kotlin, it remains open for further overriding. To stop subclasses from overriding it again, use final override.

</>
Copy
open class Parent {
    open fun display() {
        println("Parent display")
    }
}

open class Child : Parent() {
    final override fun display() {
        println("Child display")
    }
}

class GrandChild : Child() {
    // Cannot override display() here because it is final in Child.
}

This is helpful when an intermediate subclass provides a complete implementation and wants to prevent deeper subclasses from changing that behavior.

Overriding Interface Methods and Resolving Super Calls in Kotlin

Kotlin also allows a class to override methods declared in interfaces. If more than one parent type provides a method with the same name, the class must override that method and resolve the conflict explicitly.

</>
Copy
interface A {
    fun show() {
        println("A show")
    }
}

interface B {
    fun show() {
        println("B show")
    }
}

class Demo : A, B {
    override fun show() {
        super<A>.show()
        super<B>.show()
        println("Demo show")
    }
}

The syntax super<A>.show() tells Kotlin exactly which parent implementation should be called. This is required when there are multiple possible super implementations for the same method.

Common Kotlin Override Method Errors

The following mistakes are common when learning method overriding in Kotlin.

  • Forgetting open in the parent class: A class must be open before another class can inherit from it.
  • Forgetting open in the parent method: A function is final by default and cannot be overridden unless it is open.
  • Forgetting override in the child method: Kotlin requires the override keyword for an overriding function.
  • Changing the function signature: If the function name or parameters do not match, Kotlin treats it as a different function, not an override.
  • Calling the wrong super method: With multiple interfaces, use qualified super calls such as super<InterfaceName>.methodName().

Kotlin Override Method FAQs

Why do we use open before overriding a Kotlin method?

Kotlin classes and methods are final by default. The open keyword tells Kotlin that the class or method is allowed to be inherited or overridden.

Is override mandatory in Kotlin?

Yes. When a child class replaces a parent class method, Kotlin requires the override keyword. This makes the code clear and helps the compiler catch accidental signature mistakes.

Can a Kotlin method be overridden without open?

No. A member function in a class cannot be overridden unless it is declared with open, or unless it is already an overridden member that has not been marked final.

How do you call the parent version of an overridden method in Kotlin?

Use super.methodName() from inside the overriding function. If there are multiple parent implementations, use a qualified super call such as super<ParentType>.methodName().

Can an overridden Kotlin method be stopped from further overriding?

Yes. Use final override in the subclass. This allows the subclass to override the parent method but prevents its own subclasses from overriding the same method again.

Editorial QA Checklist for Kotlin Method Override Tutorial

  • Confirm that every new Kotlin overriding example uses both open and override where required.
  • Check that output blocks use the output class and do not use a programming-language class.
  • Verify that examples distinguish inherited methods from overridden methods.
  • Make sure qualified super calls are shown when multiple interface implementations are discussed.
  • Review the final explanation to ensure it does not imply that all inherited methods can be overridden by default.

Summary of Kotlin Override Method of Super Class

In this Kotlin TutorialOverride Method of Super Class, we have learnt that the function that could be overridden has to be open, and the overriding function definition in Sub class has to be same as that of in Super class. We also saw a Kotlin Example to override method of super class.