Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing attribute value from an abstract class

 
maximiliano marasso
Greenhorn
Posts: 10
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • 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 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 4565
8
  • Mark post as helpful
  • send pies
  • 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: 4565
8
  • Mark post as helpful
  • send pies
  • 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 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic