nikhil
nikhil

Reputation: 13

How to write getters and setters in kotlin?

I am new to kotlin and building a quiz app. I don't understand How do I write this java code into Kotlin? Especially the getters and setters? Also how to create both default and parameterized constructor in Kotlin?

What I did is This:

class Question {

    var question: String
    var opt1: String
    var opt2: String
    var opt3: String
    var answerno: Int

    constructor(question: String, opt1: String, opt2: String, opt3: String, answerno: Int) {
        this.question = question
        this.answerno = answerno
        this.opt1 = opt1
        this.opt2 = opt2
        this.opt3 = opt3
    }
}

Java Code Here:

public class Question {
    private String question;
    private String option1;
    private String option2;
    private String option3;
    private int answerNr;
    public Question() {
    }
    public Question(String question, String option1, String option2, String option3, int answerNr) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.answerNr = answerNr;
    }
    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    public String getOption1() {
        return option1;
    }
    public void setOption1(String option1) {
        this.option1 = option1;
    }
    public String getOption2() {
        return option2;
    }
    public void setOption2(String option2) {
        this.option2 = option2;
    }
    public String getOption3() {
        return option3;
    }
    public void setOption3(String option3) {
        this.option3 = option3;
    }
    public int getAnswerNr() {
        return answerNr;
    }
    public void setAnswerNr(int answerNr) {
        this.answerNr = answerNr;
    }
}

Upvotes: 0

Views: 7792

Answers (6)

Jenea Vranceanu
Jenea Vranceanu

Reputation: 4694

If you want absolutely the same structure as in Java class you presented here is the converted solution with all "nullabilities":

class Question {
    var question: String? = null
    var option1: String? = null
    var option2: String? = null
    var option3: String? = null
    var answerNr = 0

    constructor() {}
    constructor(
        question: String?,
        option1: String?,
        option2: String?,
        option3: String?,
        answerNr: Int
    ) {
        this.question = question
        this.option1 = option1
        this.option2 = option2
        this.option3 = option3
        this.answerNr = answerNr
    }
}

Thanks to @gidds, for pointing out that Kotlin by default generates getters and setters (for mutable properties) for each class property.

Properties are not private and declared as var because: - (var) your java code had getters and setters for each property; - (not private) your getters and setters simply return and set values without changing them.

If for example getQuestion and setQuestion used question value to perform some calculations and returned the result of calculations your converted class would look like this:

class Question {
    private var question: String? = null
    var option1: String? = null
    var option2: String? = null
    var option3: String? = null
    var answerNr = 0

    constructor() {}
    constructor(
        question: String?,
        option1: String?,
        option2: String?,
        option3: String?,
        answerNr: Int
    ) {
        this.question = question
        this.option1 = option1
        this.option2 = option2
        this.option3 = option3
        this.answerNr = answerNr
    }

    fun getQuestion(): String {
        return question + "value"
    }

    fun setQuestion(question: String) {
        this.question = question + "value"
    }
}

That is the most direct conversion.

Upvotes: -2

Niraj
Niraj

Reputation: 973

You don't need to assign getter setter as it is there by default. You can access them using question.option1.

You can use like this,

class Question(
    var question: String = "default value",
    var option1: String = "default value",
    var option2: String = "default value",
    var option3: String = "default value",
    var answerNr: Int = 0
)

This way you can assign default values.

Upvotes: 1

Sergei Zarochentsev
Sergei Zarochentsev

Reputation: 827

You can avoid getters/setters boilerplate with Kotlin Data classes.

You also can replace constructors with Fabric methods in Companion objects. More on this topic you could find in the book Effective Java "Item 1: Consider static factory methods instead of constructors " And here.

Another useful data classes feature is copy-methods. With them you can avoid creating mutable object. Immutable objects have a lot advantages over mutable. For example it's safe to use immutable objects in multithreading programming.

data class Question(
    val question: String,
    val opt1: String,
    val opt2: String,
    val opt3: String,
    val answerno: Int
) {
    companion object {

        // Fabric methods in companion objects as replace to secondary constructors
        fun fromQuestion(question: String) = Question(
            question = question,
            opt1 = "",
            opt2 = "",
            opt3 = "",
            answerno = 0
        )
    }
}

// Using Companion object fabric method
val myQuestion = Question.fromQuestion("question")

// Avoid mutable objects with copy method
val newQuestion = myQuestion.copy(
    question = "New question"
)

Upvotes: 0

Animesh Sahu
Animesh Sahu

Reputation: 8096

In kotlin getters and setters are automatically generated by the compilers, you can write all the variables into the constructor. This will generate all the getter and setters for the fields here.

class Question (
    var question: String
    var opt1: String
    var opt2: String
    var opt3: String
    var answerno: Int
)

If you want to provide a custom getter or setter, just create property inside the class:

class Question (
    question: String
    var opt1: String
    var opt2: String
    var opt3: String
    var answerno: Int
) {
    var question = question
        get() {
            // getter code (use field variable here to access this variable)
        }
        set(value) {
            // assign value to field variable, like `field = value`
        }
}

Upvotes: 2

Sourav
Sourav

Reputation: 135

get() = field        // getter 
        set(value) {         // setter 
            field = value 
        } 

Eg:-

get() = question
set(value){
question = value
}

Upvotes: 0

Gajendra24
Gajendra24

Reputation: 43

I would suggest you read up the kotlin documentation to create a model class. It has some good explanations here. https://kotlinlang.org/docs/reference/properties.html

Have a look at following plain object class.

class Question {
  var question:String
  var option1:String
  var option2:String
  var option3:String
  var answerNr:Int = 0
  constructor() {}
  constructor(question:String, option1:String, option2:String, option3:String, answerNr:Int) {
    this.question = question
    this.option1 = option1
    this.option2 = option2
    this.option3 = option3
    this.answerNr = answerNr
  }
}

If you are looking for the data class then try following way

data  class Question (
  var question:String,
  var option1:String,
  var option2:String,
  var option3:String,
  var answerNr:Int = 0
) {  
}

Upvotes: 0

Related Questions