Scala: extending a specific class with constructor parameters

I have this particular 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 want the fields in the constructor of the Son to be var and not val. How to fix it? Fields must remain constructor parameters.

UPDATE # 2

My problem is this: If I define a method in Son, this will not work if I change the value to the parameters of the Son instance.

 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.

+4
source share
2 answers

If I understand correctly, you want the name and surname fields in Son be immutable, unlike those in Person, which are vars.

It is simply not possible. The only way to do this is to overwrite:

 class Person (var name: String, var surname: String) //this won't compile class Son(override val name: String, override val surname: String) extends Person(name, surname) 

In Scala, you cannot rewrite var with val - and it's pretty obvious why - subclasses can override or add functionality to a superclass . If we hoped that vals redefined vars, we would remove the installer from the parent class for the overridden field. This violates the is-a relationship implied by inheritance.

What you can do is implement:

 trait Person { def name: String def surname: String } //this is fine now class Son(val name: String, val surname: String) extends Person //in case you want a type of person which can be changed. class MutablePerson(var name: String, var surname: String) extends Person 

Person now a sign - it just provides a way to get name or surname . Son overrides Person with vals - and therefore, you are guaranteed that no one else will change the fields in Son .

In your example, I assume that you still want to have a Person type that you can change. And you can do this using the MutablePerson class above, which allows you to change its fields.

Hope this helps!

+10
source

You must name the parameters different, because otherwise the methods in the son’s body will try to access the parameters, and not through the fields of the Person class. Scala will automatically create fields for the parameters if you access them in the body. It will work when you do something like this (which is also often used):

 class Son (_name: String, _surname: String) extends Person(_name, _surname){ def printSon = { if(name == "name")println("Error name Person") if(surname == "surname")println("Error surname Person") } } 
+6
source

All Articles