0

I have a trait Usertest in which i have an immutable variable name and a mutable variable age. While declaring I have initialized name with an empty string I made a class Student with a constructor having variable name and Student class extends Usertest.

Now I want to change the value of name whenever I make an instance of Student but I dont know how to do it.

I have tried it like this:

trait Usertest {
  val name: String = ""
  var age: Int= 12

  def setage(setage: Int) = {
    age = setage
  }

   def getAge :Int = age
   def getName : String = name

}

class Student(name : String) extends Usertest

object Main extends App {
  val st = new Student("ahsen")
  var age = st.setage(23)
  println("name  : " st.name)
  println("age : "+ st.getAge)

}

It gives this output on console:

name :

age :23

2 Answers 2

8

You did not override the name member, only defined a class constructor parameter with the same name. You should add val to the parameter:

class Student(override val name : String)

A case class would do this for you silently.

Note: Your code also includes other constructs not idiomatic in Scala, like defining getters and setters - uniform access principle makes this redundant.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is working, simply setage does not return anything (or better, it returns Unit, represented as ()).

You can achieve the whole thing in a much simpler and idiomatic way:

case class Student(name: String, var age: Int = 12)

object main extends App {
  val student = Student("ahsen")
  student.age = 23
  println(s"name: ${student.name}, age: ${student.age}") // "name: ahsen, age: 12"
}

Even better, you should avoid mutable variables altogether and simply create a new instance when you need to change a property of the object, using the copy constructor. Like so

case class Student(name: String, age: Int = 12)

object main extends App {
  val student = Student("ahsen")
  val newStudent = student.copy(age = 23)
  println(s"name: ${student.name}, age: ${student.age}") // "name: ahsen, age: 12"
  println(s"name: ${newStudent.name}, age: ${newStudent.age}") // "name: ahsen, age: 23"
}

3 Comments

"Your code is working" - I would disagree. The getname in the Usertest trait does not return the name passed to the student constructor, which is probably what was intended. On the positive side, your suggested Scala code looks really idiomatic.
@Suma "it's working" in a formal way. It returns Unit, which is not intended, I agree.
Unit is returned by a setter, which I would consider a minor stylistic problem (normally one would probably write : Unit explicitly). What I mean by "it is not working" is about getname not returning the name from student (but here I am repeating myself), which is what was a surprise for the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.