I am writring a simple code to learn inheritance in Scala by overriding values from super class in sub class::
class point(xy: Int, ry: Int) {
var x: Int = xy
var y: Int = ry
def move(dx: Int, dy: Int){
x = x + dx
y = y + dy
println (x);
println (y);
}
}
class next(override val xy: Int, override val ry: Int, val tet: Int) extends point(xy,ry){
var r: Int = tet
def move(dx: Int, dy: Int, dz: Int ){
x = x + dx
y = y + dy
r = r + tet
println ("Point x location : " + x);
println ("Point y location : " + y);
println ("Point z location : " + r);
}
}
object Demo {
def main(args: Array[String]) {
val loc = new next(10, 20, 15);
// Move to a new location
loc.move(10, 10, 5);
}
}
I used the keyword "override" while extending my super class "point" in my subclass "next", but I am getting following errors::
"value ry override nothing"
"value xy override nothing"
But I am overriding them, but when I remove the keyword, the code has no error. Is there something missing from my code?
regards,Amitesh
Your point class doesn't have xy
or ry
field/methods, so indeed you're not overriding anything. Use class point(val xy: Int, val ry: Int)
if you want these constructor arguments to be turned into methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.