• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scala -- how to override a getter

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have these two classes from a contrived example. Unfortunately it is not so contrived because I have a similar real-world situation I'm trying to figure out.

Account.scala:


UpgradedAccount.scala:


UpgradedAccount.scala fails to compile with the error:


Any thoughts on how to do this? Basically what I'm trying to do here is deal with a particular situation. When someone who originally had an account type of "0" gets an "upgraded account", they should then get a "2" value instead, regardless of whether that value has changed. The zero and two values are now equal in the new world of this particular program.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I finally got around this the following way.

Account.scala (now has a "def" not a "val")


UpgradedAccount.scala


Then I ran into trouble mixing this in. This version Member with the definition "class Member(val accountType: Int) extends Account" caused UpgradedMember to be unable to compile.

Member.scala (BAD version - compiles, but creates trouble later on...)


UpgradedMember.scala


Attempt to compile UpgradedMember.scala against Member.scala as defined above led to:



Then I changed to this version.

Member.scala (GOOD version)


In my real-world scenario, Java needs to call these...so I wrote this quick little program as a test of this to demonstrate the concept.

TestMemberUpgrades.java


This gives the output:

Stan has an account of type 0.
Upgraded Stan has an account of type 2.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic