Overriding mutators of a public super-class var

View: New views
2 Messages — Rating Filter:   Alert me  

Overriding mutators of a public super-class var

by MarkKikken :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to use mutators on a var that has been declared in a super-class :

---
class Something(val name:String,var age:Int) {

}

class SomethingElse(name:String, ageInitial: Int) extends Something(name, ageInitial) {

  private var ageInternal = ageInitial
 
  override def age:Int = {
    println ("getName call!")
    ageInternal
  }
 
  override def age_=(value:Int) = {
    println ("Setting age to " + value)
    ageInternal = value
  }
}

---

This won't compile. The compiler complains about 'method age cannot override a mutable variable'. Is this correct behavior? I mean: coming from a java background, i'm used to it being possible to override your getter/setter methods in a sub-class. It should not be necessary to modify the super-class to allow this (as i've read in this post). Or is something wrong with my code?

Mark

Re: Overriding mutators of a public super-class var

by boris_kolar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

MarkKikken wrote:
...This won't compile. The compiler complains about 'method age cannot override a mutable variable'.
I agree that overriding val/var accessor should work. It would be a very useful feature when you work with third-party code that you don't want to modify.
LightInTheBox - Buy quality products at wholesale price!