lesson 30: OOP – data class in Kotlin language

lesson 30: OOP – data class in Kotlin language

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

We have said that a class encapsulates a set of functionalities (methods) and data (properties)

In many situations, we want to store a set of data without needing to implement functionalities, in these cases the Kotlin language provides us with a structure called: data class.

Problem 1

Declare a data class called Article that stores the product code, its description, and price. Then define several objects of that data class in the main.

Project128 – Principal.kt

data class article (var code: Int, var description: String, var price: Float)

fun main (parameter: Array <String>) {
    val article1 = article (1, "potatoes", 34f)
    var article2 = article (2, "apples", 24f)
    println (article1)
    println (article2)


    val pointer = article1
    pointer.price = 100f
    println (article1)
    var article3 = article1.copy ()
    article1.price = 200f
    println (article1)
    println (article3)

    if (article1 == article3)
        println ("$article1 and $article3 are equal")
    else
        println ("Different $article1 and $article3")
    article3.price = 200f
    
    if (article1 == article3)
        println ("$article1 and $article3 are equal")
    else
        println ("Different $article1 and $article3")
}

The syntax for the declaration of a data class is:

Data class Article (var code: Int, var description: String, var price: Float)

We precede the keyword data and in the constructor, we define the properties that contain said data class.

To define objects of a data class is identical to the definition of objects of a common class:

Fun main (parameter: Array <String>) {
    Val articulo1 = Article (1, "potatoes", 34f)
    Var article2 = Article (2, "apples", 24f)

If we pass to function println, a variable of type data class shows us the name of the data class, the names of the properties and their values:

    Println (article1) // Article (code = 1, description = potatoes, price = 34.0)
    Println (article2) // Article (code = 2, description = apples, price = 24.0)

Actually, all data class has a series of basic methods: toString, copy etc., then when we pass the data class to the function println, what happens is that this function calls the toString method, the same result per screen we have if we write:

    Println (article1.toString ()) // Article (code = 1, description = potatoes, price = 34.0)

We can assign a variable to an object of a given data class:

    Val pointer = article1

Then the pointer variable has the reference to the same object referenced by article1, if we change the price property using the pointer variable:

    Price pointer = 100f

The content of article1 is now:

    Println (article1) // Article (code = 1, description = potatoes, price = 100.0)

To obtain a copy of a data type object, we must call the copy method:

    Var articulo3 = articulo1.copy ()
    Article1.price = 200f
    Println (article1) // Article (code = 1, description = potatoes, price = 200.0)
    Println (article3) // Article (code = 1, description = potatoes, price = 100.0)

The variable article3 points to an object other than the variable article1. This is seen when we modify the price property of article1 is not reflected in the price property of article3.

When we use the == operator on objects of type data class it verifies true if the contents of all its properties have stored equal value:

    If (article1 == article3)
        Println ("$article1 and $article3" are equal)
    Else
        Println ("Different $article1 and $article3")
    Article3.price = 200f
    If (article1 == article3)
        Println ("$article1 and $article3" are equal)
    Else
        Println ("Different $article1 and $article3")

Redefining methods of a data class in kotlin.

We have said that when declaring a data class we already inherit a series of methods that are useful for us to process the objects that we defined from the data class.

In Kotlin we can override any of the methods provided by a data class and define a new algorithm to it.

Problem 2

Declare a data class named Person that stores the name and age. Override the toString method to return a String with the concatenation of the name and age separated by a comma.

Project129 – Principal.kt

data class Person (var name: String, var age: Int) {
    override fun toString (): String {
        return "$name, $age"
    }
}

fun main (parameter: Array <String>) {
    var person1 = Person ("John", 22)
    var person2 = Person ("Ana", 59)
    println (person1)
    println (person2)
}

We declare the data class Person and implement the method toString that as all data class already has said method we must precede the name of the method the keyword override:

Data class Person (var name: String, var age: Int) {
    Override fun toString (): String {
        Return "$name, $age"
    }
}

In main we define two objects of type data class Person:

Fun main (parameter: Array <String>) {
    Var person1 = Person ("John", 22)
    Var person2 = Person ("Ana", 59)

When we call the function println and we pass persona1 then the toString method that we implement is executed and it shows us:

    Println (person1) // Juan, 22
    Println (person2) // Ana, 59

Recall that we can make the toString method more concise by implementing a function with a single expression:

    Override fun toString () = "$name, $age"

Proposed problem

  • Raise a data class called Given with a single property called value. Overwrite the toPrint method so that it shows as many asterisks as the value property indicates.
Solution
[/sociallocker]
data class Given (var value: Int) {
    override fun toString (): String {
        var string = ""
        for (i in 1..value)
            string = string + "*"
        return string
    }
}

fun main (parameter: Array <String>) {
    val given1 = Given (4)
    val given2 = Given (6)
    val given3 = Given (1)
    println (given1)
    println (given2)
    println (given3)
}

About The Author

Related posts

Leave a Reply