Kotlin Classes and Objects

Before we move ahead with the basic user defined data structure of classes and the ways to use them by the means of Object, let’s have a look at the fundamentals which go inside Kotlin.

Functional Programming:

It is defined as the paradigm which binds the very core concept of the program in a mathematical way. The focus in case of functional programming is on “What to solve”, rather than the generalized way of “how to solve”. It considers the software as a pure functions.

Object Oriented Programming:

Object oriented is the paradigm of programming where the focus is on the objects, the entire problem is approached by diving the problem into objects. Any object can have 2 characteristics:

  1. State
  2. Behaviour

State is defined as the property which an object can depict. If you can take an object of class Car, then property of a car can be its make, model, its year of manufacturing and so on. Let assume an object of type student, which will have state as name, id and phone number.

Behaviour can be depicted by the functionality of the particular object. Let’s take an example of class Car it can depict the behaviour as accelerating, changing gears, braking. All these event depict the functionality which the real world can depict.

If we were to derive this relation in our programming language, we will create a class as Car which will have state as make, model, brand and Year of manufacturing. The behaviour can be depicted by writing functions in the class as accelerating, changing gears, braking. Any object of this class car would be encapsulating both the state and behaviour.

OOP’s forms as a very deep concept and can be discussed with covering of pillars of whole topic, but we are here focusing on just the very basics of the same to jump start our main topic of discussion, i.e. Classes & Objects in kotlin.

Kotlin Classes and Objects

Classes

Before we create objects, we need to create Classes.

Classes are blueprint for the object which are the real world entities. In kotlin we can declare class using the class keyword like below:

class ClassName {
    // property
    // member function
    ... .. ...
}

The naming convention for the kotlin classes is similar to that of Java, which start with a capital letter of the first word and follows the same structure henceforth.

We define the body of the class by use of curly braces { }. If we need to define a class without any member function and data members, we can remove { } and declare it simply as below:

class EmptyClass

Let’s have a look at a sample class which has a field for the message:

class Message {
    String message;
    public void display(){
    	out.println(message);
    }
    public void setMessage(String message){
   	this.message=message; 
   }
}

Now here, we have a variable or property as message and it has 2 member functions as display() and setMessage().

As seen above, all our classes and functions including the getters and setters have an identifier which defines the scope & visibility of the classes and functions, we call these as Visibility identifiers.

There are 4 identifier modifiers available:

  1. public
  2. private
  3. protected
  4. default

Let’s have look at all these modifiers one by one:

Private modifier: Variables and functions which are declared as private can be accessed only from inside the class and owing to their capability of having visibility limited to the class in which the members are declared.

Public modifier: Members which are declared as public are visible though out the project and can be accessed anywhere. We can even declare classes as public and these can be accessed across the packages.

Protected modifier: All the members which need to be accessed as private class and can also be accessed in sub classes are termed as protected modifier.

Default modifier: If we are not specifying any modifier it picks default.

We will be covering about these modifiers in detail in a separate topic. Having gone through the basic details and a brief idea about modifiers, let move on to the important concept of objects.

Objects

Object represent the actual real world entity which is created for an outline or map declared by the class. Objects may be real world or it can be logical entity also.

Any java object has 2 basic characteristics:

State and behaviour

We can create object in 2 steps:

Step 1: Creation of Reference:

The very first step which comes to creation of object is the reference. Once a reference is created no memory is allocated to the object till you initialise it with new keyword.

Example:

Student st;

Step 2: Memory Allocation

As soon we assign the object to the constructor the memory is assigned to the particular class object.

st = Student ();

Accessing Members of Object:

To access the members can be done by using the DOT (.) operator.

Let’s have a look at the practical example:

We have made a class with the Student with 4 data members: name, age, gender and roll. We make an object of the class as “obj”. Now we can access the object state and methods using the dot operator, as shown in the example below:

class Student { 
    var name: String = ""
    var age: Int = 0
    var gender: Char = 'M'
    var roll: Int = 0 
  
    fun insertValues(name : String, age : Int, gender : Char, roll: Int) { 
        this.name = name 
        this.age = age 
        this.gender = gender 
        this.roll = roll 
        println("Name of the Student : $name") 
        println("Age of the Student : $age") 
        println("Gender of the Student : $gender") 
        println("Roll of the Student : $roll") 
    } 
    fun insertName(n: String) { 
        this.name = n 
    } 
} 
fun main(args: Array<String>) { 
    // creating multiple objects 
    var obj = Student() 
    // object 2 of class employee 
    var obj2 = Student() 
  
    //accessing the member function 
    obj.insertValues("KOTLIN", 24, 'M', 1) 
  
    // accessing the member function 
    obj2.insertName("Hello World ") 
  
    // accessing the name property of class 
    println("Name of the new Student : ${obj2.name}") 
}

Output:

Name of the Student : KOTLIN
Age of the Student : 24
Gender of the Student : M
Roll of the Student : 1
Name of the new Student : Hello World

With all concepts explained and used above, that precisely explains the concept of Kotlin classes and objects. Let’s have a look at the concept of data classes in the next article.

Leave a Comment

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