Lesson 28: OOP – private and public access modifiers in Kotlin

Lesson 28: OOP – private and public access modifiers in Kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

One of the fundamental principles of object-oriented programming is encapsulation, this is achieved by grouping a series of methods and properties within a class.

In Kotlin when we implement a class by default all properties and methods are public type. A public method or property can be accessed from where we define an object of that class.

In case we need to define methods and properties that can only be accessed from within the class we must define them with the private modifier.

Problem 1

Putting a class Operations that in a method requests the loading of 2 integers and later call from the same method to two other private methods that calculate their sum and product.

Project122 – Principal.kt

class Operations {
    private var value1: Int = 0
    private var value2: Int = 0

    fun load () {
        print ("Enter first value:")
        value1 = readLine () !!. toInt ()
        print ("Enter second value:")
        value2 = readLine () !!. toInt ()
        sum()
        subtract()
    }

    private fun sum () {
        val sum = value1 + value2
        println ("The sum of $value1 and $value2 is $sum")
    }

    private fun subtract () {
        val subtraction = value1 - value2
        println ("The subtraction of $value1 and $value2 is $subtraction")
    }
}

fun main (parameter: Array <String>) {
    val operations1 = Operations ()
    operations1.load()
}

A method is defined as private by preceding the keyword private:

    Private fun sum () {
        Val sum = value1 + value2
        Println ("The sum of $value1 and $value2 is $sum")
    }

Then if we want to access this method from where we define it,  an object generates a syntactic error:

Lesson 28: OOP - private and public access modifiers in Kotlin - Lesson 28: OOP - private and public access modifiers in Kotlin - Lesson 28: OOP - private and public access modifiers in Kotlin - private and public access modifiers in Kotlin
private and public access modifiers in Kotlin

The same if we want to hide the access to a property from outside the class, we must precede the keyword private:

    Private var value1: Int = 0
    Private var value2: Int = 0

The public keyword is not necessary to add it to a method or property since it is the default value that the Kotlin compiler takes when we define them. It does not generate an error, but it is redundant then to write said modifier:

Lesson 28: OOP - private and public access modifiers in Kotlin - Lesson 28: OOP - private and public access modifiers in Kotlin - Lesson 28: OOP - private and public access modifiers in Kotlin - private and public access modifiers in Kotlin
private and public access modifiers in Kotlin

Problem 2

Raise a class called Given. Define a private property called value and three private methods that draw a line of asterisks and another two public, one that generates a random number between 1 and 6 and another that prints it by calling in the latter to draw the line of asterisks.

Project123 – Principal.kt

class given {
    private var value: Int = 1
    fun throwin () {
        value = ((Math.random () * 6) + 1) .toInt ()
    }

    fun print () {
        separator()
        println ("Value of data: $value")
        separator()
    }

    private fun separator () = println ("**************************************")
}

fun main (parameter: Array <String>) {
    val given1 = given ()
    given1.throwin ()
    given1.print ()
}

We define the value property of type private:

    Private var value: Int = 1

The separator method also defines it private:

    Private fun separator () = println ("**************************************")

From the main function where we define an object of the class Given, we can only access the methods to draw and print:

Fun main (parameter: Array <String>) {
    Val given1 = Given ()
    Given1.throwin()
    Given1.print()
}

Proposed problem

  • Develop a class that defines a private property of type array of 5 integers. In the init block, call a private method that loads random values between 0 and 10.
    Define three other public methods that show the array, the largest, and the smallest element.
Solution

About The Author

Related posts

Leave a Reply