Home » Kotlin

Classes and Objects with Examples in Kotlin

In previous articles we have studied basics, arrays, functions, looping statement ranges etc. Now in this articles we will learn about classes, constructors and objects.
Submitted by Aman Gautam, on December 10, 2017

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

Constructor

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.

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.

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

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.

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

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

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 in Kotlin

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 in kotlin

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.