BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Class and Objects – Object Oriented Programming (OOP)

Last Updated: March 11, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

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:
Kotlin class objects

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Inheritance with examples
  2. Kotlin Constructors with examples
  3. Kotlin Interfaces with examples
  4. Kotlin Tutorial for Beginners | Learn Kotlin
  5. Kotlin Nested and Inner Class with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap