DEV Community

Cover image for Master Method Overloading and Overriding in Android (Kotlin) Development
supriya shah
supriya shah

Posted on

Master Method Overloading and Overriding in Android (Kotlin) Development

Originally published on Medium:
https://medium.com/@supsabhi/master-method-overloading-and-overriding-in-android-kotlin-development-46a8f2da6d07

Method overloading and method overriding are very common and core techniques in OOPS programming. When you build apps, you must have come across these terms. They sound similar, but they do completely different jobs! They let you reuse and extend code in smart ways by allowing you to write clean, reusable, and flexible code.

Overloading = same name, different parameters.
Overriding = subclass replaces/extends behavior of a parent class method.

Let’s look more deeply into each one of these.

Method Overloading
Method overloading happens when multiple methods share the same name but have different parameter lists. What it means is there are multiple functions with the same name but different signatures. These differences can be in the number, type, or order of parameters. This different signature allows the compiler to pick the correct function when you call it.

So, you can say that the main goal of overloading is to make your code more readable and flexible.

A few important things need to be considered in Kotlin are:

The compiler resolves which function to call at compile time (this is called Compile-Time Polymorphism) because it knows the parameter types beforehand.
It doesn’t require inheritance. Overloading can happen entirely within one class.
Example:


class Calculator {
    fun sum(a: Int, b: Int): Int {
        return a + b
    }
fun sum(a: Int, b: Int, c: Int): Int {
        return a + b + c
    }
    fun sum(a: Double, b: Double): Double {
        return a + b
    }
}
fun main() {
    val calc = Calculator()
    println(calc.sum(2, 3))        // uses (Int, Int)
    println(calc.sum(2, 3, 4))     // uses (Int, Int, Int)
    println(calc.sum(2.5, 3.5))    // uses (Double, Double)
}
Enter fullscreen mode Exit fullscreen mode

Here, all the methods have the same name sum, but they differ either in their parameter number or in parameter type.

In Kotlin, you might see overloading in functions like Toast.makeText()as below

Toast.makeText(Context context, CharSequence text, int duration)
Toast.makeText(Context context, int resId, int duration)
Enter fullscreen mode Exit fullscreen mode

Overloading with extension functions or top-level functions also works — Kotlin treats overloads in that scope too.
You need to be careful with ambiguous overloads: if two overloads are too similar and the compiler cannot clearly pick one, you’ll get an “overload resolution ambiguity” error.

When & why to use overloading

When you have a method that logically does the same thing but you want to support different kinds of inputs (e.g., load(image: String), load(image: Uri), load(image: File)).
When you want to improve readability: you don’t need many method names like loadStringImage, loadUriImage; you just overload load().

Method Overriding

Overriding is about inheritance. You have a base (parent) class that defines a method (or abstract method), and a subclass (child class) provides its own implementation of that method (same signature) so that when you call it on the subclass instance, the subclass version runs. This supports runtime polymorphism. You’re essentially taking the parent’s generic instruction and giving it a specific, custom implementation in the child.

As Android development is built on inheritance, you constantly override functions that the Android system (the parent) defines.
When you create an Android Activity, you inherit the onCreate() method from a parent class like AppCompatActivity.
The parent has a default onCreate() that handles basic setup. You override it to add your specific code.

Also in Kotlin:

Classes and methods are final by default (i.e., cannot be inherited/overridden) unless you mark them with open.
In the subclass, you must use the override keyword to indicate you are overriding.
You can call the parent version via super.methodName() if needed.
For example, all lifecycle methods like onCreate(), onStart(), onPause() are open in the Activity class, so you can override them.

class MainActivity : AppCompatActivity() {
    override fun onPause() {
        super.onPause() // calls the original method from AppCompatActivity
        // your custom logic here
        Log.d("MainActivity", "App is paused")
    }
}
Enter fullscreen mode Exit fullscreen mode

Here:
override → tells Kotlin we’re redefining a method from the parent.
super.onPause() → calls the original version in the parent class (AppCompatActivity), to keep the Android lifecycle working properly.
Then, we can add our own logic below or above that call.

So we can say that when overriding an Android lifecycle method (like onCreate), you often call super.onCreate(...) inside your overridden function.
This is you saying, “Do the Parent’s basic setup first, and then run my special code.”

Why to use overriding

  • To customize or extend behavior of a base class method in a subclass.
  • To implement interfaces or abstract classes: you provide concrete implementations in subclasses.

The Quick Summary: The Difference That Matters
Think of it this way:

Overloading is about variety (one name, many parameter options). It happens horizontally within one class. It lets you create multiple versions of the same method with different inputs.
Overriding is about specialization (one name, one parameter set, one new action). It happens vertically down the inheritance chain. It lets you modify how inherited methods behave.
Mastering these two concepts is key to writing clean, logical, and truly Object-Oriented Kotlin code in your Android applications!

Top comments (0)