Inheritance vs Extension functions

We all know about inheritance which is there in the OOPs concept where one can extend a class and add functionality to it. In the new programming languages, there is something called as extension functions which can add a functionality to an existing class without extending it.

This post was originally posted at https://agrawalsuneet.github.io/blogs/inheritance-vs-extension-functions/ and reposted on Medium on 3rd Dec 2019.

Sometimes we are confused about what to use when. To get help here, one needs to understand first what are these two and what are the benefits and limitations over the other. Once you understand the difference, it will be very easy to decide what to use.

Let’s understand with code first what are both. I am going to explain using Kotlin language example but the concept is the same across all the languages.

Inheritance

Here is an example.

open class Person(var firstName : String,
protected var lastName : String) {
open fun getFullDetails() : String {
return "$firstName $lastName"
}
}
class Student : Person {
var rollNumber = 0

constructor(firstName : String,
lastName: String,
rollNumber : Int)
: super (firstName, lastName){
this.rollNumber = rollNumber
}

override fun getFullDetails() : String {
return "$firstName $lastName with Roll number as $rollNumber"
}
}

And we can call it by using

var student = Student ("Suneet", "Agrawal", 12)
println(student.getFullDetails())

Here we added a property i.e. rollNumber and override the function.

Now let's have a look at almost similar functionality using Extension functions.

Extension Function

class Employee(var firstName : String, var lastName : String) {
}
fun Employee.getFullDetails() : String {
return "$firstName $lastName"
}

We have a similar kind of class which is not open (final class). we added a function getFullDetails as an extension function to the employee class and we call the above function as

var emp = Employee("Suneet", "Agrawal")
println(emp.getFullDetails())

Now that we understand what is extension functions and what is an inheritance, let try to find out the difference which will make our life really easy while choosing what to use when.

Merits of Extension functions

  • Can add functionality without subclassing that means the object reference and the implementation can be of your base class.

Demerits of Extension functions

  • Can’t add any new variable/properties in your extension functions. You can add a property by just writing getter and setter but only without backing field. So basically you can’t add a field in the base class.

Merits of Inheritance

  • Can access all protected variables and functions of the base class.

Demerits of Inheritance

I hope this made clear on what are the merits and demerits of extension functions and inheritance. Please note that the extension functions are resolved statically.

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store