Kotlin - Extension

In this chapter, we can learn Kotlin - Extension.

You can add without do away with inheriting or editing them

Function Extension

Kotlin lets in to outline a technique out of doors of the principle magnificence

Let's see following example how the extension is applied at the practical level.

class Alien 
   var talents : String = "null"
   a laugh printMySkills() 

a laugh essential(args: Array) 
   var  a1 = Alien()
   a1.Abilties = "Play"
   var  a2 = Alien()
   a2.Capabilities = "Read"
   var  a3 = Alien()
   a3.Skills = a1.AddMySkills(a2)

fun Alien.AddMySkills(a:Alien):String
   var a4 = Alien()
   a4.Skills = this.Capabilities + " " +a.Talents
   go back a4.Capabilities

In the above example, we don’t have any approach inner “Alien” elegance named as "addMySkills()" Output
Play Read

Object Extension

Using this mechanism, we will create an item of a category internal a factory technique and later we can simply name that method the use of the reference of the magnificence name. In the subsequent instance, we will create a “partner item”.

amusing predominant(args: Array) 

magnificence A 
   associate object 
      a laugh display():String 
         return("You are mastering Kotlin from Welookups.Com")
The above piece of code will yield the following output inside the browser.

You are getting to know Kotlin from Welookups.Com