Kotlin is an object oriented programming language just like Java. Object oriented programming (OOP) allows us to solve the complex problem by using objects. In this guide, we will learn what is a class, what is an object and several other features of Object Oriented Programming (OOP).
Kotlin Class
Classes are the main building blocks of any object oriented programming language. All objects are part of a class and share the common property and behaviour defined by the class in form of data members and member functions respectively.
A class is like a blueprint for the objects.
A class is like a prototype for objects which you can create by grouping methods and variables.
How to define a class in Kotlin
A class is defined using class
keyword in Kotlin. For example –
class MyClass { // variables or data members // member functions .. .. }
Kotlin class Example
In the following example we have a class Example
and in that class we have a data member number
and a member function calculateSquare()
. Data member also known as property and member function as behaviour in the object oriented programming language terms. The objects of this class share these properties and behaviours.
class Example { // data member private var number: Int = 5 // member function fun calculateSquare(): Int { return number*number } }
I have not specified any access modifier in the above class. Access modifiers restrict the access. By default the access modifier is public
. In the above example we have not specified any access modifier so by default public
access modifier is applicable for the above class Example
The data member in the above class is specified as private
which means the data member number
is not accessible outside the class Example.
How to define access modifiers?
Access modifiers are specified before the class keyword.
private class MyClass { }
Other Access modifiers:
private – Can be accessed inside the class only.
public – Can be accessed everywhere.
protected – Can be accessed to the class and its subclasses.
internal – Can be accessed inside the module.
Kotlin Object
Objects use the properties and behaviours of the class. As mentioned above, class is just a blueprint, no memory is allocated to the class. Once the objects of the class are created they take memory space and perform various actions on the data using data members and members functions of the class.
How to create object of class
Lets say my class name is Example
, the object of this class can be created like this:
Example e = Example()
Note: Here I have named the object as e
, you can give any name to object except the already defined keywords in the Kotlin. Another point to note here is that unlike java where we use new keyword to create the object, we do not use the new keyword here, in fact if you use it, you will get a compilation error.
How to access the data members and member functions
To access the data member number
and member function calculateSquare
of the class Example
using the object e
.
//Access data member e.number //Access member function e.calculateSquare()
Lets take a complete example to understand the above discussed concepts.
Kotlin object example
class Example { // data member private var number: Int = 5 // member function fun calculateSquare(): Int { return number*number } } fun main(args: Array<String>) { // create obj object of Example class val obj = Example() println("${obj.calculateSquare()}") }
Output:
Leave a Reply