Functions Kotlin

Class, Primary Constructor, Secondary Constructor and Init Block

fun main(args: Array<String>) { var student = Student("Steve", 10) println(student.id) } class Student(var name: String) { var id: Int = -1 init { println("Student has got a name as $name and id is $id") } constructor(n: String, id: Int): this(n) { // The body of the secondary constructor is called after init block this.id = id } }

Inheritance

fun main(args: Array<String>) { var dog = Dog() dog.bread = "labra" dog.color = "black" dog.bark() dog.eat() var cat = Cat() cat.age = 7 cat.color = "brown" cat.meow() cat.eat() var animal = Animal() animal.color = "white" animal.eat() } open class Animal { // Super class / Parent class / Base class var color: String = "" fun eat() { println("Eat") } } class Dog : Animal() { // Sub class / Child class / Derived class var bread: String = "" fun bark() { println("Bark") } } class Cat : Animal() { // Sub class / Child class / Derived class var age: Int = -1 fun meow() { println("Meow") } }

by default Class are:

*public

*final

So for inheritance you need to make a class --OPEN--

Override

fun main(args: Array<String>) { var dog = MyDog() println(dog.color) dog.eat() } open class MyAnimal { open var color: String = "White" open fun eat() { println("Animal Eating") } } class MyDog : MyAnimal() { var bread: String = "" override var color: String = "Black" fun bark() { println("Bark") } override fun eat() { super<MyAnimal>.eat() println("Dog is eating") } }

Overriding Member Functions and variable

Just like Kotlin classes, members of a Kotlin class are also public and final by default.

To allow a member function to be overridden, you need to mark it with the open modifier.

Moreover, The derived class that overrides a base class function must use the override modifier, otherwise, the compiler will generate an error

Inheritance with Primary and Secondary Constructors

fun main(args: Array<String>) { var dog = TheDog("Black", "Pug") } open class TheAnimal { // Super class / Parent class / Base class var color: String = "" constructor(color: String) { this.color = color println("From Animal: $color") } } class TheDog : TheAnimal { // Sub class / Child class / Derived class var bread: String = "" constructor(color: String, breed: String): super(color) { this.bread = breed println("From Dog: $color and $breed") } }

Inheritance with Primary and Secondary Constructors

Visibility Modifiers

open class Person { // Super class private val a = 1 protected val b = 2 internal val c = 3 val a = 10 } class Indian: Person() { // Sub class fun printTest() { print(a) // a is not visible // b, c, d are visible } } class TestClass { fun testing() { var person = Person() print(person.a) // a, b are not visible // c, d are visible } }

public always is visible

protected visible inside sub class, not visible inside other class

internal visible from module

private just visible from yourClass.kt

Abstract Class

fun main(args: Array<String>) { // var person = MyPerson() // Not allowed. You cannot create instance of abstract class var person = Indian() // Allowed. Abstract Super class reference variable // pointing to child class object. person.name = "Steve" person.eat() person.goToSchool() } abstract class MyPerson { // you cannot create instance of abstract class abstract var name: String abstract fun eat() // abstract properties are 'open' by default open fun getHeight() {} // A 'open' function ready to be overridden fun goToSchool() {} // A normal function } class Indian: MyPerson() { override var name: String = "dummy_indian_name" override fun eat() { // Our own code } }

The Role or Abstract class is to just provide set of methods and properties

Abstract Class are Partlally defined class

Abstract Methods have no body when declared

Abstract Property cannot be initialized when declared

CONCLUSION

you cannot create intance/objects of ABSTRACT class

you need to override ABSTRACT methods, propertics inside Derived class

Interface

fun main(args: Array<String>) { var myButton = MyButton() myButton.onTouch() myButton.onClick() } interface MyInterfaceListener { // You cannot create the instance of interface fun onTouch() // Methods in interface are abstract by default fun onClick() { // Normal methods are public and open by default but NOT FINAL println("MyInterfaceListener: onClick") } } interface MySecondInterface { // You cannot create the instance of interface fun onTouch() { // Normal Method println("MySecondInterface: onTouch") } fun onClick() { // Normal methods are public and open by default but NOT FINAL println("MySecondInterface: onClick") } } class MyButton: MyInterfaceListener, MySecondInterface { override fun onTouch() { super<MyInterfaceListener>.onClick() super<MySecondInterface>.onClick() } override fun onClick() { super.onTouch() } }

Interface can contains both NORMAL methods and ABSTRACT methods

But they contain only ABSTRACT PROPERTY

Interface is not class

You can not create instance of an INTERFACE

similar to an ABSTRACT class

Data Clases

fun main(args: Array<String>) { var user1 = User("Sam", 10) var user2 = User("Sam", 10) println(user1.toString()) if (user1 == user2) println("Equal") else println("Not equal") var newUser = user1.copy(id = 25) println(newUser) } data class User(var name: String, var id: Int)

Any class contains functions such as:

equals():Boolean

hasCode():Int

toString:String

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

Kotlin creates a copy() too

Sealed

sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() object NotANumber : Expr() fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) NotANumber -> Double.NaN // the `else` clause is not required because we've covered all the cases }

Sealed classes are used for representing restricted class hierarchies

when a value can have one of the types from a limited set, but cannot have any other type

They are, in a sense, an extension of enum classes:

- the set of values for an enum type is also restricted,

- but each enum constant exists only as a single instance

- whereas a subclass of a sealed class can have multiple instances which can contain state