To begin with, let's see a very simple example:
- We will create a very simple class Student, and we will create an extension function for it:
class Student(val age:Int)
- Now, we would want to create an isAgeGreaterThan20 function, which will return true if the age is greater than 20, or else, will return false. Now suppose there's a restriction that we can't touch the Student class, what will you do?
- In those scenarios, extension functions come in handy when you want to extend the functionality of a class. If you try to call the method, you will be shown an error, as follows:
- You then need to select the Create extension function ...