File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance and variables

 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to post the code here, it will help in understanding the problem in better way.
 
K Riaz
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does use the local variable. Prehaps your code is wrong?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, variables aren't polymorphic, only methods are.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

My code should it help anyone else following this thread:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic