Kotlin Class

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Kotlin class

Objects are created from classes. A class is a blueprint for an object; it shares common
properties and behaviour in form of members and member functions.

In Kotlin, a class is declared with the class keyword.

The class declaration consists of the class name, the class header (specifying its type
parameters, the primary constructor etc.) and the class body. The class body is specified inside
two curly braces. Both the header and the body are optional. If the class has no body, curly
braces can be omitted.

Kotlin simple class example


In the following example, we create a simple object from a class.

class Simple {

private val name = "Simple"


fun info() = "This is $name class"
}

fun main() {

val s = Simple()
println(s.info())
}
Output: This is Simple class

In the example, we declare a Simple class and later create an object from it.
class Simple {

private val name = "Simple"


fun info() = "This is $name class"
}
A class in Kotlin is created with the class keyword. Inside our class, we have one property
(member) and one function.
val s = Simple()
A new object is created from the Simple class. In Kotlin, we don't use the new keyword to create
an instance.
println(s.info())

Using the dot operator, we call the object's info function.


An instance of the class is instantiated in the following way:
val firstClass = FirstClass()
var new = FirstClass() //here new is the name of the var.

Contrary to Java, new isn’t a keyword in Kotlin. Classes by default are final in Kotlin. So the equivalent of the
above definitions in Java would like something like this:

public final class FirstClass {


}

Hence, by default classes in Kotlin are not inheritable. To make a class non-final we need to append the
keyword open.

open class Me{


}

The open annotation allows others to inherit from this class.

Let’s create a class with a few functions and a property. We’ll see how to access the functions and properties
of that class. Furthermore, we’ll see how to set the member properties.

class User {

var loggedIn: Boolean = false


val cantChangeValue = "Hi"

fun logOn() {
loggedIn = true
}

fun logOff() {
loggedIn = false
}
}

fun main() {

val user = User()


println(user.loggedIn) //false
user.logOn()
println(user.loggedIn) //true
user.logOff()
println(user.loggedIn) //false
user.cantChangeValue = "Hey" //won't compile. Can't modify a final variable.

}
Kotlin empty class
In the following example, we create two empty classes.
class Being {}
class Empty

fun main() {

val b = Being()
println(b)

val e = Empty()
println(e)
}

An empty class has no members or member functions. The curly brackets can be omitted.

Kotlin primary constructor


A Kotlin class can have a primary constructor and one or more secondary constructors. The
primary constructor is part of the class header: it goes after the class name (and optional type
parameters).
class User constructor(name: String, email: String) { }
Here we use the constructor keyword.
class User (name: String, email: String) { }

If the primary constructor does not have any annotations or visibility modifiers (such as public),
the constructor keyword can be omitted.

The primary constructor cannot contain any code. The initialization code can be placed in
initializer blocks, they are created with the init keyword.

Kotlin program of primary constructor with initializer block –

It acts as an initialiser block where member variables are initialised. This block gets executed whenever an
instance of this class is created.

There can be multiple init blocks and they are called in the order they are written inside the class.

fun main() {
val person = Person("Mahmood")
}
class Person {
constructor(name : String){
println("This is constructor")
}
init{
println("This is first init block")
}

init{
println("This is second init block")
}
init{
println("This is third init block")
}
}

Output:

This is first init block


This is second init block
This is third init block
This is constructor

Explanation: When the object person is created for the class Person, the value “Mahmood” passed to the
parameters name of the constructor. Two properties are declared in the class id and name.
Initializer block is executed at the time of object creation, and not only initialize the properties but also prints to
the standard output.

Default value in primary constructor –


Similar to functions default values in functions, we can initialize the constructor parameters with some default
values.
Kotlin program of default values in primary constructor –
fun main() {
val emp = employee(18018, "Sagnik")
// default value for emp_name will be used here
val emp2 = employee(11011)
// default values for both parameters because no arguments passed
val emp3 = employee()

}
class employee(emp_id : Int = 100 , emp_name: String = "abc") {
val id: Int
var name: String

// initializer block
init {
id = emp_id
name = emp_name

print("Employee id is: $id, ")


println("Employee name: $name")
println()
}
}

Output:
Employee id is: 18018, Employee name: Sagnik
Employee id is: 11011, Employee name: abc
Employee id is: 100, Employee name: abc
Explanation: Here, we have initialized the constructor parameters with some default values emp_id = 100 and
emp_name = “abc”.
When the object emp is created we passed the values for both the parameters so it prints those values.
But, at the time of object emp2 creation, we have not passed the emp_name so initializer block uses the default
values and print to the standard output.

//class User constructor (name: String, email: String) {


class User(name: String, email: String) {

private val name = name


private val email = email

override fun toString(): String {

return "User $name has email $email"


}
}
fun main() {

val u = User("Peter Novak", "pnovak47@gmail.com")


println(u)
}

In the example, we have a User class with a primary constructor. We have two members and
one overriden function.
User Peter Novak has email pnovak47@gmail.com

Secondary Constructors

Secondary Constructors are written inside the body of the class by prefixing with the keyword
constructor. Following example demonstrates the same.

class Student {
var name: String
val age : Int

constructor(name: String, age: Int)


{
this.name = name
this.age = age
}

fun printDetails()
{
println("Name is $name and Age is $age")
}

fun main() {

var student = Student("Anupam", 24)


student.printDetails()
}
//Following is printed in the console.
//Name is Anupam and Age is 24

The most common usage of secondary constructors comes in subclasses when you need to initialize the class in
different ways. If the class contains a primary constructor, the secondary constructor must refer to it in its
declaration. The declaration is done using this keyword.

Kotlin open class


Kotlin classes are final by default. Other classes cannot inherit from a final class. To make a
class inheritable, we mark it with the open keyword.
open class Being(private val alive: Boolean = true) {

fun isAlive(): Boolean {

return alive
}
}

class ABC(val name: String): Being() {

fun def(): String {

return "HELLO HELLO"


}
}

fun main() {

val d = ABC("Abhishek")

println(d.def())
println(d.name)
println(d.isAlive())
}

We have an open Being class. The ABC class inherits from Being.
println(d.isAlive())

We can call the isAlive function on the ABC class, because it was inherited from its parent
Being class.

HELLO HELLO
Abhishek
true

Kotlin data class


Some classes are desinged to hold data. With data classes, we can considerably reduce the code.
Compiler automatically creates the equals, hashCode, toString, and copy functions.

A data class in Kotlin is created with the data keyword.

The data classes must follow a couple of rules. The primary constructor needs to have at least
one parameter. All primary constructor parameters must be marked as val or var. The data
classes cannot be abstract, open, sealed or inner.

data class User(val name: String, val email: String)

fun main() {

val u = User("Peter ", "pnovak47@gmail.com")


println(u)

println(u.name)
println(u.email)

val (name, email) = u;


println("$name $email")

val u2 = User("Peter ", "pnovak47@gmail.com")

println(u == u2)
println(u === u2)
}

In the example, we have one data class: User.


data class User(val name: String, val email: String)

A data class is prefixed with the data keyword.


val u = User("Peter", "pnovak47@gmail.com")
println(u)

Here we call the toString method, which has been created for us.
val (name, email) = u;
println("$name $email")

We can extract the properties from the data class using desctructuring declaration.
User(name=Peter, email=pnovak47@gmail.com)
Peter
pnovak47@gmail.com
Peter pnovak47@gmail.com
true
false
Kotlin nested class
A nested class is declared inside another class.
class Outer {

val name = "Outer"


fun show() = "the name: $name"

class Nested {
val name = "Nested"
fun show() = "the name: $name"
}
}
fun main() {
println(Outer().show())
println(Outer.Nested().show())
}

In order to access the nested class, we specify the name of its outer class. So the show function
of the nested class is invoked like this: Outer.Nested().show. A nested class cannot access the
members of the outer class.

Kotlin inner class


Inner classes are created with the inner keyword. Unlike nested classes, they can access the
members of their outer classes.
class Outer {
val name1 = "Outer"
fun show() = "the name: $name1"
inner class Inner {
val name2 = "Inner"
fun show() = "data: $name2 and $name1"
}
}
fun main() {

println(Outer().show())
println(Outer().Inner().show())
}
In the example, we have one inner class. Its show function outputs the name1 member of the
Outer class.

Output:
the name: Outer
data: Inner and Outer

Kotlin abstract class


An abstract class, member, or member function is created with the abstract keyword. If a class
inherits from an abstract class, it must implement all its abstract members and member
functions. The implemented members and member functions must be prefixed with the
override keyword. We cannot create an instance from an abstract class. Abstract classes are
implicitly open, since they are useless if they don't have any concrete subclasses.

An abstract class is conceptually similar to an interface. Unlike an interface, an abstract class


can have state. While a class can implement multiple interfaces, it can inherit only from one
abstract class.
Abstract classes are used when building a hierarchy of classes; they serve as a grouping of some
shared functionality for related classes.
abstract class Shape() {
abstract var w: Int
abstract var h: Int

abstract fun area(): Int

fun info(): String {


return "width: $w; height: $h"
}
}
class Rectangle(override var w: Int, override var h: Int): Shape() {

override fun area(): Int {


return w * h;
}
}

fun main() {

val r = Rectangle(5, 6)
println(r.area())
println(r.info());
}
Output:
30
width: 5; height: 6

In the example, we have an abstract Shape class. It is not possible to create a shape, because it is
too generic. We can only create a descendant of a shape; for instance, a rectangle.
abstract var w: Int
abstract var h: Int

abstract fun area(): Int

Here we have two abstract variables and an abstract function. We must provide an
implementation for them in the subclass.
Kotlin sealed class
A sealed class is used for representing restricted class hierarchies. A value can have one of the
types from a limited set, but cannot have any other type. Sealed classes are more powerful enum
classes.

Sealed classes in Kotlin are implemented in the following manner.


sealed class A{
class B : A()
class C : A()
}

To specify a sealed class, you need to add the modifier sealed. A sealed class cannot be
instantiated. Hence, are implicitly abstract. The following WON’T work.
fun main(args: Array<String>)
{
var a = A() //compiler error. Class A cannot be instantiated.
}

Constructors of a sealed class are private by default. All subclasses of a sealed class must be
declared within the same file.

Sealed classes are abstract and can have abstract members; this means that they cannot be
instantiated directly. Sealed classes cannot have public constructors (The constructors are
private by default). Sealed classes can have subclasses, but they must either be in the same file
or nested inside of the sealed class declaration.
sealed class Shape
class Circle(var radius: Float) : Shape()
class Square(var width: Int) : Shape()
class Rectangle(var width: Int, var height: Int) : Shape()

fun getArea(e: Shape) =


when (e) {
is Circle -> println("Circle area is ${Math.PI * e.radius * e.radius}")
is Square -> println("Square area is ${e.width * e.width}")
is Rectangle -> println("Rectangle area is ${e.width * e.height}")
}

fun main() {
val circle = Circle(7f)
val square = Square(5)
val rectangle = Rectangle(8, 6)

getArea(circle)
getArea(square)
getArea(rectangle)
}
Output:
Circle area is 153.93804002589985
Square area is 25
Rectangle area is 48

In the example, we have a sealed Shape class. It has three subclasses: Circle, Square, and
Rectangle.

fun getArea(e: Shape) =


when (e) {
is Circle -> println("Circle area is ${Math.PI * e.radius * e.radius}")
is Square -> println("Square area is ${e.width * e.width}")
is Rectangle -> println("Rectangle area is ${e.width * e.height}")
}

The getArea function calculates the area for a shape. Note that the else statement is not needed,
since the compiler knows that the list of options is exhaustive.

Enum Class
In programming, sometimes there arises a need for a type to have only certain values. To
accomplish this, the concept of enumeration was introduced. Enumeration is a named list of
constants.
In Kotlin, like many other programming languages, an enum has its own specialized type,
indicating that something has a number of possible values. Unlike Java enums, Kotlin enums
are classes.

Some important points about enum classes in kotlin –

 Each of the enum constants acts as separate instances of the class and separated by
commas.
 Enums increases readability of your code by assigning pre-defined names to constants.
 An instance of enum class cannot be created using constructors.

 Enums are defined by using the enum keyword in front of class like this:-
enum class DAYS{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
Initializing enums –

In Kotlin also enums can have a constructor like Java enums. Since enum constants are
instances of an Enum class, the constants can be initialized by passing specific values to the
primary constructor.

Here is an example to specify colors to cards –


enum class Cards(val color: String) {
Diamond("black"),
Heart("red"),
}

We can easily access the color of a card using –

val color = Cards.Diamond.color

Enums properties and methods –

As in Java and in other programming languages, Kotlin enum classes has some inbuilt
properties and functions which can be used by the programmer. Here’s a look at the major
properties and methods.

Properties –

1. ordinal: This property stores the ordinal value of the constant, which is usually a zero-based index.
2. name: This property stores the name of the constant.

Methods –

1. values: This method returns a list of all the constants defined within the enum class.
2. valueOf: This methods returns the enum constant defined in enum, matching the input string. If the constant, is
not present in the enum, then an IllegalArgumentException is thrown.

Example to demonstrate enum class in Kotlin –

enum class DAYS {


SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
fun main()
{
// A simple demonstration of properties and methods
for (day in DAYS.values()) {
println("${day.ordinal} = ${day.name}")
}
println("${DAYS.valueOf(" WEDNESDAY ")}")
}
Output:

0 = SUNDAY
1 = MONDAY
2 = TUESDAY
3 = WEDNESDAY
4 = THURSDAY
5 = FRIDAY
6 = SATURDAY
WEDNESDAY

Enum class properties and functions –

Since enum class in Kotlin, defines a new type. This class type can have its own properties and functions. The
properties can be given a default value, however, if not provided, then each constant should define its own
value for the property. In the case of functions, they are usually defined within the companion objects so that
they do not depend on specific instances of the class. However, they can be defined without companion objects
also.

Example to demonstrate properties and functions in Kotlin

enum class DAYS(val isWeekend: Boolean = false){

SUNDAY(true),

MONDAY,

TUESDAY,

WEDNESDAY,

THURSDAY,

FRIDAY,

// Default value overridden

SATURDAY(true);

fun main(){

// A simple demonstration of properties and methods

for(day in DAYS.values()) {

println("${day.ordinal} = ${day.name} and is weekend ${day.isWeekend}")

}
Output:

0 = SUNDAY and is weekend true


1 = MONDAY and is weekend false
2 = TUESDAY and is weekend false
3 = WEDNESDAY and is weekend false
4 = THURSDAY and is weekend false
5 = FRIDAY and is weekend false
6 = SATURDAY and is weekend true

Usage of when expression with enum class –

A great advantage of enum classes in Kotlin comes into play when they are combined with the when
expression. The advantage is since enum classes restrict the value a type can take, so when used with the when
expression and the definition for all the constants are provided, then the need of the else clause is completely
eliminated. In fact, doing so will generate a warning from the compiler.

enum class DAYS{

SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}

fun main(){

when(DAYS.SUNDAY){

DAYS.SUNDAY -> println("Today is Sunday")


DAYS.MONDAY -> println("Today is Monday")
DAYS.TUESDAY -> println("Today is Tuesday")
DAYS.WEDNESDAY -> println("Today is Wednesday")
DAYS.THURSDAY -> println("Today is Thursday")
DAYS.FRIDAY -> println("Today is Friday")
DAYS.SATURDAY -> println("Today is Saturday")
// Adding an else clause will generate a warning
}

Output:

Today is Sunday

Singleton Class
Singleton Class in Kotlin is also called as the Singleton Object in Kotlin. Singleton class is a class that is defined in such a
way that only one instance of the class can be created and used everywhere. Many times we create the two different
objects of the same class, but we have to remember that creating two different objects also requires the allocation of
two different memories for the objects. So it is better to make a single object and use it again and again.

fun main()
{
val obj1 = GFG()
val obj2 = GFG()
println(obj1.toString())
println(obj2.toString())
}

class GFG
{

}
In the above example, we can see that both the objects have a different address, thus it is a wastage of memory. In the
below program, we have done the same thing, but we have used the singleton class.

// Kotlin program
fun main()
{
println(GFG.toString())
println(GFG.toString())
}
// GFG is the singleton class here
object GFG
{

So when we use an object instead of a class, Kotlin actually uses the Singelton and allocates the single memory. In Java, a
singleton class is made making a class named Singleton. But in Kotlin, we need to use the object keyword. The object
class can have functions, properties, and the init method. The constructor method is not allowed in an object so we can
use the init method if some initialization is required and the object can be defined inside a class. We call the method and
member variables present in the singleton class using the class name, as we did in the companion objects.

// Kotlin program
fun main()
{
println(GFG.name)
println("The answer of addition is ${GFG.add(3,2)}")
println("The answer of addition is ${GFG.add(10,15)}")
}

object GFG
{
init
{
println("Singleton class invoked.")
}

var name = "GFG Is Best"


fun add(num1:Int,num2:Int):Int
{
return num1.plus(num2)
}
}
Output:

Singleton class invoked.


GFG Is Best
The answer of addition is 5
The answer of addition is 25

Properties of Singleton Class

The following are the properties of a typical singleton class:

 Only one instance: The singleton class has only one instance and this is done by providing an instance of the
class, within the class.
 Globally accessible: The instance of the singleton class should be globally accessible so that each class can use it.
 Constructor not allowed: We can use the init method to initialize our member variables.

You might also like