• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime binding and garbage collection

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Save this program in the file myover.java and see the output.

The output is 5. Which means this is the value contained
in the object created at line #1.
After Line #3 the reference to the object created at
Line #1 is lost, since this reference is now refering
to the object created at Line#2.
So from where we are getting the value '5' at line#4 which is
the value in the object created at line#1 ?
Regards
Sanjay
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future. Learn more about UBB codes - Ajith]

[This message has been edited by Ajith Kallambella (edited September 08, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A subclass can have a variable with the same name as the variable in the parent class. However, this is called shadowing the parent class variable, not overriding. Have a look at below example:
class Base extends Object {
int count;
public void intcount() { count ++;}
}
class XtndBase entends Base {
int count;
public void intcount() {count++;}
}
XtndBase xb = new XtndBase();
Base b = xb;//casting to parent class
b.intcount()// uses the XtndBase method
int x = b.count// gets the Base variable, not XtndBase.
 
Sanjay Mishra
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dilip for u'r explanation.
But my doubt us that how the base object's variable
is obtained once we assign the reference of
base class object to derived class object.
Hope I am clear.
Does not a=b means we are losing the reference of base
class object? If yes than from where the value contained
in base class object is getting printed.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjay;
Yes, the object <code>a</code> is unreachable at line 3, and <code>a</code> now refers to an object of type <code>B</code>. The behavior of your code is explained by the difference between inheritance of fields and that of methods.
Every object of type <code>B</code> has the following fields:
  • <code>int i //declared in B</code>
  • <code>int j //declared in B</code>
  • <code>int A.i //declared in A, inherited by and hidden but present in B</code>
  • <code>int A.j //declared in A, inherited by and hidden but present in B</code>

  • Inherited fields are really there in subclass objects, not just referred to; they've got allocated storage and everything. This is the principle difference between inheritance of methods and fields. Think about why - this is how a subclass object can assign to and read it's own copy of inherited fields. Bodies of inherited methods don't need to be physically present in subclasses; the run time method call resolution mechanism finds the actual code to execute by searching superclasses.
    Hidden inherited fields may be accessed in several ways; your <code>println</code> call accesses them through a superclass reference (reference of type <code>A</code>). If you wanted to access the subclass fields declared in <code>B</code> through a reference of type <code>A</code>, you'd have to downcast the reference.
    For the full story, see JLS2 8.3
    BTW, this behavior isn't shadowing, it's hiding. Shadowing is when an identifier in a nested scope has the same name as another identifier in an enclosing scope, as in:
    <pre><code>
    class X {
    int i;
    void f() {
    float i; //shadows the instance variable i
    }
    }
    </code></pre>
    For the full scoop on shadowing, see JLS2 again.

    [This message has been edited by jply (edited September 09, 2000).]
     
    Sanjay Mishra
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much jply.
    I am more than convinced.
    Regards
    Sanjay
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    jply,
    Thanks for correcting me.
     
    Ranch Hand
    Posts: 3141
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    jply,
    How would you downcast the reference?
    I've tried (B)a.i and a.(B.i) ... both cause compile errors. Is there a way to actually retrieve the original value assigned to b.i once it's been assigned to a Class A reference?
    Thanks
    [This message has been edited by Jane Griscti (edited September 09, 2000).]
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Jane;
    Use <code>((B)a).i</code>
    One of my weak spots is expression evaluation order, especially in cases like this. I just throw enough parentheses in to be sure to express what I want. Sloppy, I know...
     
    Jane Griscti
    Ranch Hand
    Posts: 3141
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks jply. That worked.
     
    Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic