aspose file tools*
The moose likes Beginning Java and the fly likes inheritance and variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance and variables" Watch "inheritance and variables" New topic
Author

inheritance and variables

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have a class called Current

which has a variable called:

private double limit;

a method then uses this variable
i.e. System.out.println("Limit is set to: "+limit);

I have another class called Platinum which extends current
this has a variable called:
private double limit = 100;

Why in my output am I geting the following line:
Limit is set to 0.0 for both the Current and Platinum classes?

I thought if it was a private variable it would take the variable local to that class i.e. limit would be 0.0 for Current and 100 for Platinum?
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
You might want to post the code here, it will help in understanding the problem in better way.


SCJP 1.4<br />SCWCD 1.3
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
It does use the local variable. Prehaps your code is wrong?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

new B().getX()

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:



[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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.

Thanking you in advance!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

HTH

Layne


Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Keep Coding!

Layne
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many thanks for your response.

Firstly, I tried to implement Ilja Preuss suggestions.



When trying to compile this I got the following error:
C:\java\BankAccount>javac Current.java
Current.java:51: missing method body, or declare abstract
protected Current (int limit);
^
1 error

As it is NOT a method but a constuctor I am declaring I can't have a missing method body and I do not wish to declare it abstract as I wish to implement it in the Current class.

Can anyone help with suggestions on what I am doing wrong?

I also tried implementing Surasak Leenapongpanit suggestion:

In my Current class I have the following code:


and in my Platinum class I have the following code:


here, I get the following error message when compiling:

C:\java\BankAccount>javac TestClass.java
.\Platinum.java:42: cannot resolve symbol
symbol : constructor Current ()
location: class Current
{
^
1 error

Thanking you in advance for your help!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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!
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

ALL, many thanks for your help! Got there in the end.

My code should it help anyone else following this thread:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance and variables
 
Similar Threads
Quadratic perplextion
super Constructor
Inheritance: Abstract or Interface
Removing multiple if conditions
extending a class