I have this concrete class:
class Person (var name: String, var surname: String)
and I want to create another class that extends Person:
class Son (name: String, surname: String) extends Person(name, surname)
OK. But I do want the fields in the constructor of Son must be var and not val. How do I fix this? The fields must remain constructor parameters.
UPDATE #2
My problem is as follows: If I define a method in Son, this does not work if I change the value to the parameters of an instance of Son.
class Son (name: String, surname: String) extends Person(name, surname){
def printSon = {
if(this.name=="name")println("Error name Person")
if(this.surname=="surname")println("Error surname Person")
}
}
object main {
def main(args: Array[String]): Unit = {}
val Marco = new Son("Marco","Bianchi")
Marco.printSon // So far everything is ok
Marco.name="name"
Marco.printSon // Here in control is not done, because Marco.name="name" writes in Person
println("FINE")
}
name e surname in Son are of type val.
Son. Do you maybe mean the opposite? or what is it that you want to achieve really?