Kotlin – Class and Object

Kotlin – Class and Object

In this section, we will take in the nuts and bolts of Object-Oriented Programming (OOP) utilizing Kotlin. We will find out about class and its article and how to play with that object. By meaning of OOP, a class is an outline of a runtime element and item is its state, which incorporates the two its conduct and state. In Kotlin, class presentation comprises of a class header and a class body encompassed by wavy supports, like Java.

Class myClass {// class Header  
 
// class Body 
} 

Like Java, Kotlin likewise permits to make a few objects of a class and you are allowed to incorporate its class individuals and capacities. We can control the perceivability of the class individuals factors utilizing distinctive watchwords that we will learn in Chapter 10 – Visibility Control. In the accompanying precedent, we will make one class and its item through which we will get to various information individuals from that class.

class myClass { 
    // property (data member) 
    private var name: String = "softwarelogy.com" 
 
    // 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() 
}

The above bit of code will yield the accompanying yield in the program, where we are calling printMe() of myClass utilizing its very own item.

You are at the best Learning website Named-softwarelogy.com

Nested Class

By definition, when a class has been made inside another class, at that point it is called as a settled class. In Kotlin, settled class is of course static, thus, it tends to be gotten to without making any object of that class. In the accompanying precedent, we will perceive how Kotlin deciphers our settled 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 softwarelogy.com" 
          
    } 
} 

The above bit of code will yield the accompanying yield in the program.

Welcome to The softwarelogy.com

Inner Class

At the point when a settled class is set apart as an “internal”, at that point it will be called as an Inner class. An internal class can be gotten to by the information individual from the external class. In the accompanying model, we will get to the information individual from the external 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 softwarelogy.com" 
    inner class Nested { 
        fun foo() = welcomeMessage 
    } 
}

The above bit of code will yield the accompanying yield in the program, where we are calling the settled class utilizing the default constructor given by Kotlin compilers at the season of incorporating.

Welcome to the softwarelogy.com

Anonymous Inner Class

Unknown internal class is a quite decent idea that makes the life of a developer simple. At whatever point we are executing an interface, the idea of mysterious inward square comes into picture. The idea of making an object of interface utilizing runtime object reference is known as mysterious class. In the accompanying precedent, we will make an interface and we will make an object of that interface utilizing Anonymous Inner class instrument.

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() 
}

The above bit of code will yield the accompanying yield in the program.

I am an example of Anonymous Inner Class

Type Aliases

Type monikers are a property of Kotlin compiler. It gives the adaptability of making another name of a current kind, it doesn’t make another sort. In the event that the sort name is excessively long, you can without much of a stretch present a shorter name and utilize the equivalent for future utilization. Type pseudonyms is extremely useful for complex sort. In the most recent variant, Kotlin renounced the help for sort assumed names, in any case, on the off chance that you are utilizing an old rendition of Kotlin you may have use it like the accompanying

typealias NodeSet = Set<Network.Node> 
 
typealias FileTable<K> = MutableMap<K, MutableList<File>> 

Leave a Reply

Your email address will not be published. Required fields are marked *