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