Kotlin Classes and Objects

By IncludeHelp Last updated : December 04, 2024

Kotlin Class

A class may be defined as,

  • blueprint for an object.
  • collection of similar types of objects.
  • template which contains various methods and variable declaration.

Syntax:

class ExBoy{
...
}

A class can be defined using class keyword followed by 'class name' and then class header(primary constructor) if necessary. Then the class body is defined in curly braces. Ifclass has no body then the curly braces can also be omitted. class without body looks like class ExBoy

Kotlin Constructors

Constructor is a special member function that is used to initialize the objects. They are used to set the values to the members of the class for a particular object. In kotlinthere are two types of constructor.

1. Primary Constructor

A class can have only one primary constructor. They are defines along with the class name in the class header.Primary constructor cannot contain any code, if any initialization needed then it can be placed in init(initialization) block.

Example

class ExBoy(name :String){

}

class ExBoy(name :String){
  var name:String
  init{
    this.name=name;
  }      
}

2. Secondary Constructor

This constructor is defined using constructor() function. This is same constructor as we define in java. If a class contain both primary and secondary constructor, then secondary constructor needs to acknowledge primary constructor either directly or using this keyword.

Example

class ExBoy{
  var name:String
  constructor(name :String){
    this.name=name;
  }
}

Note: in kotlin, getter and setter properties are already included in the compiler. It is not necessary to explicitly defined get() and set() function. We can create custom get() and set() methods also.

Create an instance of class

To create an instance of a class, use the following syntax:

Syntax

var ob=ExBoy("Aman")

It will create an object ob of ExClass and variable name will be initialized with "Aman". Here, new keyword is not used in kotlin as there is no new keyword in kotlin.

Getting Values

To get Values, use (.) operator with object and class member.

println(ob.name)  //same as ob.getName() in java

Setting Values

To set value, use (=) sign with new value to set the variable.

ob.age=19//same as ob.setAge(19) in java

Class Visibility Modifiers

Visibility modifiers can help the programmer to restrict the access of class members.There are 4 type of access specifiers in kotlin:

  1. Public - This is default access modifier for functions and variables in Kotlin. Public members can be accessed from anywhere in the program.
  2. Private - Members that are declared with private access modifier can only be accessed by other members of the same class.
  3. Protected - this is similar to private member access function but difference is that these can be accessible within the class and also in its subclass.
  4. Internal - In projects with multiple modules, the members declared in a module can only be accessed within same module.

Program to illustrate class with constructor overloading

class ExBoy{
  var name:String
  var age:Int=0

  constructor(name :String){
    this.name=name;
  }
  
  constructor(name:String,age:Int){
    this.name=name
    this.age=age
  }
}

fun main(str:Array<String>){
  var ob=ExBoy("Aman")

  println(ob.name)  //same as ob.getName() in java
  ob.age=19          // same as ob.setAge(19) in java
  println(ob.age)

  var o2=ExBoy("Deepak",16)
  print("${o2.name},${o2.age}")

}

Output

Aman
19
Deepak,16

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.