Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - CLASSES & OBJECTS


Anything and everthing you see around, can be said to be a class. Be it a Car or a Tree or even a Human.


A class can be described as a blueprint which says how an object is going to behave once it is created. And a class has a behaviour and a state.


Let us simplify with the below example.


As we all know, we being Humans, have a common behavior of speaking and eating.


Now, just imagine, when God had created the Human Beings, God had prepared a blue print stating, all Humans will have a common behavior of speaking and eating.

java_Collections

So, speaking and eating behaviour would be common in all Human Beings. But each Human Being will have a property, that would makes them unique.


i.e. Say, a guy named John who speaks English and eats Burger is one Person. And a girl named Rakhi who speaks Hindi and eats Rice is a different Person.


So, let's say, God had also made this rule called along with the Behaviour, a Human Being should also have property/state(i.e. Name, language, food) that will make them unique.

java_Collections

Now, if we compare the above Human as a class. The name, food and language are called as property/state that will be unique for a Human Being.


And the Eat and Speak are behaviour that will be common to all Human Beings.


Now, let us create two humans out of the above class. i.e. A guy named John who speaks English and eats Burger and a girl named Rakhi who speaks Hindi and eats Rice.

java_Collections

So, Human is a Class that is just a Blue print and doesn't exist physically.


And human1 and human2 are called as Objects that exist physically (i.e. The John and Rakhi are humans that have physical existence) and are derived from the above class Human.


Now, let us see, how can we implement Classes and Objects in Kotlin.


Example :



class Human{

    var name: String = ""
    var food: String = ""
    var language: String = ""

    fun eat(){
        println(name+" Eats "+food)
    }

    fun speak(){
        println(name+" Speaks "+language)
    }
}

fun main() {

    var human1 = Human()
    human1.name = "John"
    human1.food = "Burger"
    human1.language = "English"

    var human2 = Human()
    human2.name = "Rakhi"
    human2.food = "Rice"
    human2.language = "Hindi"


    human1.eat()
    human1.speak()

    human2.eat()
    human2.speak()
}


Output :



  John Eats Burger
  John Speaks English
  Rakhi Eats Rice
  Rakhi Speaks Hindi

So, in the above example, we have created a class named Human.


class Human { ... }

The syntax of declaring a class is quite simple, keyword class followed by the class name(i.e. Human) and the body of the class must be inside Braces {}

java_Collections

And as we have seen that humans have a common behavior of speaking and eating.


And the behaviours in Kotlin can be represented by Functions.


And next we have the eat(...) behaviour/Function. Whenever a Function is defined inside a class, it is called as method.


fun eat(){
	println(name+" Eats "+food)
}

Similarly, we have the speak(...) behaviour/method.


fun speak(){
	println(name+" Speaks "+language)
}

And then we have the properties/state name, food and language(Detail explanation later).


var name: String = ""
var food: String = ""
var language: String = ""

Note : It is mandatory to initialise the properties in the class declaration itself.


So, we have created the class Human.

java_Collections

Now, just remember, a class has no existence of its own. It only comes to effect once an Object is created out of it.


And we will see next, how can we create an Object of Human class.


var human1 = Human()

Creating an object is also pretty straight forward. You give an object name, human1 and initialise with the class name with brackets.

java_Collections

So, human1 object is created. And human1 object is for the guy named John who speaks English and eats Burger.


human1.name = "John"
human1.food = "Burger"
human1.language = "English"
java_Collections


Then we create the next object human2.


var human2 = Human()

And human2 object is for the girl named Rakhi who speaks Hindi and eats Rice.


human2.name = "Rakhi"
human2.food = "Rice"
human2.language = "Hindi"
java_Collections


So, the Objects, human1 and human2 of the class Human has the above values.


Now, if we want to get the values of the Objects, human1 and human2. We need to call the methods, eat() and speak().


human1.eat()

So, the eat() method,


fun eat(){
	println(name+" Eats "+food)
}

Of human1 object is called.


human1.eat()

And the print statement,


	println(name+" Eats "+food)

Goes to the human1 object and finds that, name is Rakhi and food is referring to Rice.


So we get the output,


Rakhi eats Rice

And exactly same logic applies to the other lines as well.


human1.speak()
human2.eat()
human2.speak()

What about the Getters and Setters?


Java and C++ programmers might be wondering, where are the Getters and Setters?


Well! In Kotlin, Getters and Setters are auto generated.


When you define a property called name is defined,


var name: String = ""

Getters and Setters are auto generated by Kotlin.