• 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

Changing attribute value from an abstract class

 
Greenhorn
Posts: 10
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have an abstract class with an private attribute and it corresponding getter and setter methods to modify its value. I'm trying to modify the value of this attribute from subclasses of this abstract class but somehow it's not working. This is the output I get after running this code:

It seems that MyClass isn't changing cadena attribute.
What am I doing wrong? What concept am I missing?
Here's the code for you to see. All classes are in the same package


Thanks in advance
Regards
Maxi
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's working as I'd expect it to. What output were you expecting?
 
maximiliano marasso
Greenhorn
Posts: 10
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:It's working as I'd expect it to. What output were you expecting?



Well, in fact I was expecting MyClass2 to get the updation MyClass did. My goal is to have a common variable (a common state) that all subclasses can use and in turn add their own. Suppose that instead of cadena being a simple String attribute it's a Map. This Map is populated by the abstract class (MyAbstractClass) with some initial keys. Then as the application progresses the subclasses use existing keys and add new ones.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in MyClass2 you don't try and update the variable anywhere. You do in MyClass - showMyMessage() has a call to setCadena. But there's no similar call in the implementation in MyClass2.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, hang on, I've just realised what you mean. Every subclass will inherit the variable (and the setter and getter), but each instance has its own copy of the variable. It's no different to having a member variable in a class without inheritance.

If you want the variable to be shared between all instances of the class, you need to declare it as static.
 
maximiliano marasso
Greenhorn
Posts: 10
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Ah, hang on, I've just realised what you mean. Every subclass will inherit the variable (and the setter and getter), but each instance has its own copy of the variable. It's no different to having a member variable in a class without inheritance.

If you want the variable to be shared between all instances of the class, you need to declare it as static.



Thanks a lot, Matthew. That solved my question. Somehow I misslooked that huge detail.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic