Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Function Extension

class Alien{ 
 var skills : String= "null" 
 fun printMySkills(){ 
  print(skills) 
 }   
} 
fun main(args: Array<String>) { 
 var  a1= Alien() 
 a1.skills="JAVA" 
 //a1.printMySkills() 
  
 var  a2= Alien() 
 a2.skills="SQL" 
 //a2.printMySkills() 
  
 var  a3= Alien() 
 a3.skills=a1.addMySkills(a2) 
 a3.printMySkills() 
} 
fun Alien.addMySkills(a:Alien):String{ 
 var a4=Alien() 
 a4.skills=this.skills + " " +a.skills 
 return a4.skills 
}

Interfaces

interface A {  
    fun printMe() { 
        println(" method of interface A") 
    } 
}
interface B  { 
    fun printMeToo() { 
        println("I am another Method from interface B") 
    } 
}  
// implements two interfaces A and B 
class multipleInterfaceExample: A, B  
fun main(args: Array<String>) { 
    val obj = multipleInterfaceExample()  
    obj.printMe() 
    obj.printMeToo() 
}

Interface

interface ExampleInterface  {  
    var myVar: Int   // abstract property  
    fun absMethod():String          // abstract method 
    fun hello() { 
        println("Hello there, Welcome to TutorialsPoint.Com!") 
    } 
}  
class InterfaceImp : ExampleInterface {  
    override var myVar: Int = 25 
    override fun absMethod() = "Happy Learning " 
}  
fun main(args: Array<String>) { 
    val obj = InterfaceImp()  
    println("My Variable Value is = ${obj.myVar}") 
    print("Calling hello(): ")  
    obj.hello()  
    print("Message from the Website-- ") 
    println(obj.absMethod()) 
}

Inheritance

import java.util.Arrays  
open class ABC{ 
 fun think (){ 
  print("Hey!! i am thiking ") 
 } 
} 
class BCD: ABC(){ // inheritence happend using default constructor  
  
}  
fun main(args: Array<String>) { 
 var  a=BCD() 
 a.think() 
} 

Constructor

fun main(args: Array<String>) {  
    val HUman = HUman("TutorialsPoint.com", 25)  
    print("${HUman.message}"+"${HUman.firstName}"+"Welcome to the example of Secondary  constructor, Your Age is-${HUman.age}") 
}  
class HUman(val firstName: String, var age: Int) { 
 val message:String ="Hey!!!" 
 constructor(name : String , age :Int ,message :String):this(name,age){ 
   
   
 } 
}

Constructor Keyword

fun main(args: Array<String>) {  
    val person1 = Person("TutorialsPoint.com", 15)  
    println("First Name = ${person1.firstName}") 
    println("Age = ${person1.age}") 
}  
class Person(val firstName: String, var age: Int) {  
} 

Anonymous Inner Class

fun main(args: Array<String>) { 
    var programmer :Human = object:Human // creating an instance of the interface 
                            { 
                                override fun think(){ // overiding the think method 
                                    print("I am an example of Anonymous Inner Class ") 
                                } 
                            } 
                     
    programmer.think() 
} 
interface Human{ 
    fun think() 
}

Inner Class

fun main(args: Array<String>) { 
    val demo = Outer().Nested().foo() // caling nested class method 
    print(demo) 
   
} 
class Outer { 
    private val welcomeMessage: String = "Welcome to the TutorialsPoint.com" 
    inner class Nested { 
        fun foo() = welcomeMessage 
    } 
}

Nested Class

fun main(args: Array<String>) { 
    val demo = Outer.Nested().foo() // caling nested class method 
    print(demo) 
   
} 
class Outer {  
    class Nested { 
        fun foo() = "Welcome to The TutorialsPoint.com" 
          
    } 
}

Calling Printme

class myClass { 
    // property (data member) 
    private var name: String = "Tutorials.point"  
    // member function 
    fun printMe() { 
        print("You are at the best Learning website Named-"+name) 
    } 
}  
fun main(args: Array<String>) {  
    val obj = myClass() // create obj object of myClass class 
    obj.printMe() 
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.