• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Brogden's JAVA 2 - Chapter 8 Question 3

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created two programs based on Chapter 8 Question 3. See the following programs. I noticed that shadowing the base class avgWeight is OK but the answer says it is not. Can anyone explain why?
public class GenericFruit extends Object {
protected float avgWeight = 10;
protected float caloriesPerGram;
String varietyName;
// class definition continues with methods
}
public class Apple extends GenericFruit {
private float avgWeight;
public static void main(String argv[]) {
GenericFruit f;
Apple a = new Apple();
a.avgWeight = 20;
f = a;
System.out.println(f.avgWeight );
System.out.println(a.avgWeight );
}
}
[This message has been edited by Zheng Huang (edited January 13, 2001).]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of the few questions that gets into best practices for OO programming. Note that the actual question asks "Which of the following would be reasonable variable declarations ...."
Since Applet already "isA" GenericFruit, it inherits avgWeight. It would be legal Java to shadow the variable in a derived class but not good OO practice. (Also, I can guarantee you some very odd bugs if you wrote a program this way, due to the fact that variable addresses are calculated at compile time.)
Bill

------------------
author of:
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi William,
What do you mean by saying that "variable addresses are calculated at compile time"?
Does that mean that derived class's object changes the base class's variable instead of its own variable?
i.e., new Apple().avgWeight=20; this implies that GenericFruit class's avgWeight variable becomes 20 rather than
Apple class's avgWeight variable ???

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Acually
f=a statement is equalent to
GenericFruit f = new Apple();
that means f is a super class reference to subclass object a.
Now Acoording to superclass refrence rules
--Superclass refrence determines the varible.
--Subclass object determines the method.
Note: here if u call a method which has been overridden by ur subclass
Apple.and u did same i.e
f=a;
f.amethod();
then overridden method of subclass will execute.because method is determined by
underlying subclass object.
i hope u will get it.
regards
-------
Munish Dabra
--------
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"What do you mean by saying that "variable addresses are calculated at compile time"?
Does that mean that derived class's object changes the base class's variable instead of its own variable?"
No. Lets say your base class has a variable
float aveWt = 10.0f ;
And you have a method in the base class:
public float totalCalories( float calPerOz ){
return aveWt * calPerOz ;
}
Now suppose you create a derived class that has a variable with the same name:
float aveWt = 25.0f ;
And in a method of the derived class you call the totalCalories method in the base class. Which aveWt do you suppose will be used?
The answer is the base class variable will be used because when the base class was compiled, the address of the aveWt variable was calculated and used in the totalCalories method.
Bill
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Bill & Munish have give a good explanation.
To sum up things on Variable Shadowing I would like to state following:

The following distinction between invoking instance methods on an object and accessing instance variables of an object must be noted.
"When a method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the type of the reference, that determines which method implementation will be executed."
"When a variable of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which variable will actually be accessed."
Regards,

Raj.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic