Variables don't "override" eachother; they do "hide" eachother in certain circumstances, but that's not relevant here.
I suspect we're talking about this:
If you've got these two classes, then
returns "0", not "100", because, as I said, variables don't "override" one another. The getX() method knows how to get the variable "x" out of the "A" part of the B object, and is completely unaware of the second "x" defined in class B.
You may want to either just make "x" in A protected, or else do something like this:
With other words, variables aren't polymorphic, only methods are.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Oct 04, 2004
As always, many thanks for your response. Much appreciated!
I now acknowledge that variables are not inherited like methods.
I also tried making the limit variable protected but this made no difference so I'm wondering if I have understood things properly. To make things clear I have decided to put the code I have so far.
I have a class called current:
The output I am getting from the above, in all three classes is:
Current: Limit is: 0.0 amount is: 300.0 newBalance is: 295.55
Platinum: Limit is: 0.0 amount is: 20.0 newBalance is: 67.99
Student: Limit is: 0.0 amount is: 20.0 newBalance is: 600.0
What I expected to see was Limit in Platinum being 500.0?
So reading the response:
You may want to either just make "x" in A protected
I changed the variable limit in a to be protected: Current Class protected limit = 0.0; and got the same results i.e. all stating Limit is: 0.0 when outputing to screen?
Please note, that I know I could just simply change the argument name in the withdrawal method to include limit but one of the constraints I have is that the method return type, argument name and data types have been given and this does not include limit.
When you make the variable protected in your base class, you need to remove all declarations for variables with the same name from the base classes. The problem is that when you set the value of limit in the Platinum constructor or a method in the Platinum class, it sets the limit variable that belongs to that class. However, when you print the variable from a method in the Current class, it reads the variable from that class instead, not from the Platinum class. So to fix the problem, just remove the declaration from the Platinum class and change the declaration in the Current class from private to protected. Doing so will allow the variable to be shared across the two classes.
Layne's solution would work, but it also breaks encapsulation in a way that I wouldn't be comfortable with.
As far as I understand, the limit as a fixed property of the subclass? In those cases, I prefer to provide a constructor in the base class to define that property:
The advantage of this approach is that in class B you don't need to care about how the value of x is stored in the base class.
Does that help?
Joined: Dec 06, 2001
I totally agree with Ilja's comments above. I often use code similar to Ilja's example in my own programs. It is a much more elegant solution than my suggestion above. My intent in the above post was primarily to clarify an earlier comment about making the variable protected. I wasn't really thinking about alternative solutions.
The first thing you did is the right thing to do. Just get rid of that semicolon that appears to make the constructor into an "abstract; there's no such semicolon in Ilja's example. Also if you look back you'll see that I gave you the same example in my earlier reply!
Joined: Oct 04, 2004
ALL, many thanks for your help! Got there in the end.
My code should it help anyone else following this thread: