Monday, 18 December 2017

Kotlin: classes and objects

Objects and class in Real World scenario

Object
An Object is a real-world entity. For example, book, cat, box, bus, keyboard, person, screen, cell phone etc., all are objects.

How can we differentiate objects
1. Depend on their properties
2. Depend on their behaviors

Differentiating two objects based on their properties
Let’s say, there are two persons A and B.

First let’s try to find what are the possible properties for the person object

Possible properties for a person
height, weight, color, Education Qualification, country, address are all come under person properties.

Now by comparing these properties we can easily differentiate two persons.
Properties
Person A
Person B
Height
6ft
5.5ft
Weight
75Kg
60Kg
Color
White
Black
Country
India
America

As shown in the above table, both the persons has their own properties, So we can differentiate based on them.

Differentiating two objects based on their behavior
consider the same two persons A and B. will try to figure out possible behaviors for the person object

Possible behaviors are
walk, run, swim, eat, sleep, see, hear etc., Now by comparing these behaviors we can easily differentiate two persons.

Behavior
Person A
Person B
Walk
10km/hr
12km/hr
run
20km/hr
18km/hr
sleep
8 Hrs/day
6Hrs/day
Behavior
Person A
Person B

Class
In normal terms I call class as a category.

All persons come under Human category
cat, lion, Elephant come under Animals Category

So a class or category is a group of objects with similar properties and behaviors.

Objects and class in Programming Terminology

What Is an Object?
An object is a software bundle of related state and behavior. The main advantage of Kotlin is you can compare objects in Kotlin with real world objects. So it is very easy to work with objects in Kotlin.

What Is a Class?
A class is a blueprint or prototype from which objects are created.

You can define a class using 'class' keyword.

Syntax
class ClassName{
 // Properties
 // Functions
}

Example

class Employee {
 var firstName : String = ""
 var lastName : String = ""
}

Above snippet define 'Employee' class, it has two properties (firstName, lastName) defined.

How to define object to the class Employee?
var emp1 = Employee()
Above statement is used to define an object to the class Employee.

How to access the properties associated with object emp1?
Members of the object (firstName and lastName) are accessed using ‘.’ Notation.

Syntax
objName.propertyName

Example
emp1.firstName = "Chamu"
emp1.lastName = "Gurram"


Employee.kt
class Employee {
 var firstName: String = ""
 var lastName: String = ""
}


Main.kt

fun main(args: Array<String>) {

 var emp1 = Employee()
 emp1.firstName = "Chamu"
 emp1.lastName = "Gurram"

 println("First name : ${emp1.firstName}")
 println("Last name : ${emp1.lastName}")
}


Output

First name : Chamu
Last name : Gurram







Previous                                                 Next                                                 Home

No comments:

Post a Comment