Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Kotlin Class and Objects

Class
Object
Kotlin Nested class and Inner class

Ranjit Patnaik Kotlin OOP-Concept 1 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Content

1 Kotlin Class and Objects

2 Class

3 Object
Create an object-
Class and Object Example

4 Kotlin Nested class and Inner class

Ranjit Patnaik Kotlin OOP-Concept 2 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Kotlin Class and Object

Kotlin supports both functional and object-oriented programming. In


previous articles, we have learned about functions, higher-order functions
and lambdas which represents Kotlin as a functional language. Here, we
will learn about the basic OOPs concepts which represent Kotlin as
Object-Oriented programming language.

Ranjit Patnaik Kotlin OOP-Concept 3 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Class

Like Java, class is a blue print for the objects having similar properties. We
need to dene a class before creating object and class keyword is used to
dene a class. The class declaration consist of class name, class header and
class body enclosed with curly braces.
Syntax of class declaration:
class className { // class header
// property
// member function
}

Class name: every class has a specic name


Class header: header consist of parameters and constructors of a class
Class body: surrounded by curly braces, contains member functions and other property.
Both the header and the class body is optional; if there is nothing in between curly braces
then class body can be omitted.
Ranjit Patnaik Kotlin OOP-Concept 4 / 10
Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Object

It is a basic unit of Object Oriented Programming and represents the


real-life entities, which has state and behavior. Objects are used to access
the properties and member function of a class. In Kotlin, we can create
multiple objects of a class. An object consists of :
State : It is represented by attributes of an object. It also reects the
properties of an object.
Behavior : It is represented by methods of an object. It also reects
the response of an object with other objects.
Identity : It gives a unique name to an object and enables one object
to interact with other objects.

Ranjit Patnaik Kotlin OOP-Concept 5 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Create an object-

We can create an object using the reference of the class.


var obj = className()

Accessing the property of the class- We can access the properties of class using an object. First
create an object using the class reference then access the property.

obj.nameOfProperty

Accessing the member function of class-


We can access the member function of the class using the object.

obj.funtionName(parameters)

Ranjit Patnaik Kotlin OOP-Concept 6 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Class and Object Example

class Student{ //class Header


//properties-Data Members
var name: String = ""
var roll_no : String = ""
var Sem : Int = 0
//member function
fun getData(n: String,r: String, s: Int){
name = n
roll_no = r
Sem = s
println("Name of the Student: $name")
println("Roll No. of the Student: $roll_no")
println("Semester of the Student: $Sem")
}
}
fun main(){
var s1 = Student()
s1.getData("Aryan","19CSE204",6)
}
Ranjit Patnaik Kotlin OOP-Concept 7 / 10
Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Kotlin Nested class and Inner class

Nested Class
A class is declared within another class then it is called a nested class. By
default nested class is static so we can access the nested class property or
variables using dot(.) notation without creating an object of the class.
Syntax of declaration:
class outerClass {
............
// outer class properties or member function

class nestedClass {
..........
// inner class properties or member function
}
}

Ranjit Patnaik Kotlin OOP-Concept 8 / 10


Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Kotlin program of accessing nested class properties

// outer class declaration


class outerClass {
var str = "Outer class"
// nested class declaration
class nestedClass {
val firstName = "Praveen"
val lastName = "Ruhil"
}
}
fun main(args: Array<String>) {
// accessing member of Nested class
print(outerClass.nestedClass().firstName)
print(" ")
println(outerClass.nestedClass().lastName)
}

In Kotlin, to access the member function of nested class, we need to create the object for nested
class and call the member function using it.
Ranjit Patnaik Kotlin OOP-Concept 9 / 10
Kotlin Class and Objects
Class
Object
Kotlin Nested class and Inner class

Kotlin program of accessing nested class member function

// outer class declaration


class outerClass {
var str = "Outer class"
// nested class declaration
class nestedClass {
var s1 = "Nested class"
// nested class member function
fun nestfunc(str2: String): String {
var s2 = s1.plus(str2)
return s2
}
}
}
fun main(args: Array<String>) {
// creating object of Nested class
val nested = outerClass.nestedClass()
// invoking the nested member function by passing string
var result = nested.nestfunc(" member function call successful")
println(result)
}
Ranjit Patnaik Kotlin OOP-Concept 10 / 10

You might also like