aspose file tools*
The moose likes Java in General and the fly likes Changing attribute value from an abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Changing attribute value from an abstract class" Watch "Changing attribute value from an abstract class" New topic
Author

Changing attribute value from an abstract class

maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

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

Joined: Apr 06, 2010
Posts: 4465
    
    8

It's working as I'd expect it to. What output were you expecting?
maximiliano marasso
Greenhorn

Joined: May 31, 2011
Posts: 10

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

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: May 31, 2011
Posts: 10

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Changing attribute value from an abstract class