Lynda-2


Classes

1. You can define more than one class like java.
2. By default, classes are public which is opposite of java.
3. New keyword is not needed, enum is soft keyword
3. Class define with class keyword.
4. Class have primary constructor and multiple secondary constructor.
5. Extension of kotlin file is .kt.
6.

**************************************************

Create Class
----------------------------------------------
class SecondClass {

}
-----------------------------------------------


Create Counstructor
----------------------------------------------

class SecondClass constructor(firstNAme : String, secondName : String) {

}
-----------------------------------------------

Create Counstructor without keyword - bcz constructor has annotation and visibility modifier
----------------------------------------------

class SecondClass (firstName : String, secondName : String) {

}
-----------------------------------------------



Init - If we want to execute code inside the class then we need initializer function.
----------------------------------------------

class SecondClass (firstName : String, secondName : String) {

init{
        println("My Name is $firstName $secondName")
}

}
-----------------------------------------------

Secondary Constructor -
1. If you want to make secondary constructor then it's mandatory to call first constructor .
2. If you are using visibility modifier like internal then must use 'constructor' keyword before body.
-----------------------
class Person(firstName : String, lastName : String) {

    init {
        println("My Name is $firstName $lastName")
    }

    constructor(firstName : String, lastName : String, middleName : String) : this(firstName , lastName ){
    }
}

----------------------

----------------------------------------------------------------------------------------


******************************************************************

Object

Create an object with some property
-----------
val adHoc = object { var x: Int = 0 var y: Int = 0 } print(adHoc.x + adHoc.y)
----------

Create an object with some function
-----------  -------------------
val location = object {
    var xPosition = 200    var yPosition = 400
    fun printIt(){
        println("Position = ($xPosition, $yPosition})")
    }
}

location.printIt()
----------  ------------------

You can change property value
-----------  -------------------

val location = object {
    var xPosition = 200    var yPosition = 400
    fun printIt(){
        println("Position = ($xPosition, $yPosition})")
    }
}

location.printIt()
location.xPosition = 2000location.yPosition = 4000location.printIt()
----------  ------------------

Object declaration is statement not an expression
---------- ------------------- --------------------
object mySingleton{

    var temperatures = arrayOf(20, 40, 70)

    fun getLastTemperature() : Int{
         return temperatures.last()
    }
}

fun main(args: Array<String>) {

    val temperature = mySingleton.getLastTemperature()
    print(temperature)

}

---------- ------------------- --------------------



******************************************************************

******************************************************************
Interface

Some Point

1. It same like as Jova interface
2. We can declare property and function in interface
3. If we implement Interface in class then override property and function which don't have body.

Create Interface -
--------
interface Vehicle{

    val myUnicornName : String

    fun  funWithBody(){
        print("Cantain body, not mandatory to override")
    }

    fun  funWithoutBody() : Int

}
--------

Create Car class - To override method and property
--------
class Car : Vehicle {
    override val myUnicornName: String = "BMW"
    override fun funWithoutBody(): Int {
       return 500    }


}
--------

Calling in main method
--------
val myCar = Car()
println(myCar.myUnicornName)
println(myCar.funWithoutBody())
myCar.funWithBody()
--------

******************************************************************

Generic - It allows us to do things like assigning a subclass instance to superclass:

-------------

fun main(args: Array<String>) {

    val maxInt : Int = max(23, 77)
    val maxLong : Long = max(123456789L, 999999999L)
    val maxByte : Byte = max(23.toByte(), 77.toByte())
    val maxDouble : Double = max(23.66, 776.65)
    val maxString : String = max("Hello", "India")

    println("Max of Int : $maxInt")
    println("Max of Int : $maxLong")
    println("Max of Int : $maxByte")
    println("Max of Int : $maxDouble")
    println("Max of Int : $maxString")

}

------------
max file class
------------
fun <T : Comparable<T>> max(param1 : T, param2 : T) : T{

    val  results = param1.compareTo(param2)
    return if(results > 0) param1 else param2
}

Note - 
1. T (Type) - It is super class of all  datatype like. - Int, String
2. Comparable<T> : provide method to comparable type, here used because 'compareTo' method doesn't work with all type
------------

==========
As in Java, classes in Kotlin may have type parameters:
class Box<T>(t: T) {
    var value = t
}
In general, to create an instance of such a class, we need to provide the type arguments:
val box: Box<Int> = Box<Int>(1)
But if the parameters may be inferred, e.g. from the constructor arguments or by some other means, one is allowed to omit the type arguments:
val box = Box(1) // 1 has type Int, so the compiler figures out that we are talking about Box<Int>



22

Comments

Popular Posts