Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

shadowing variables

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class First {
public String str = "first string";
public String value() {
return this.str;
}
public String str1 = value();
}
class Second extends First {
public String str = "second string";
public String value() {
return this.str;
}
public String str1 = value();
public static void main(String h[]){
First first = new Second();
System.out.println( "first.value() = " + first.value() );
System.out.println( "first.str = " + first.str );
System.out.println( "first.str1 = " + first.str1 );
}
}

//********output
first.value() = second string
first.str = first string
first.str1 = null
Process terminated with exit code 0

Why did first.str1 get assigned to null?
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class First {
public String str = "first string";
public String value() {
return this.str;
}
public String str1 = value();
}
class Second extends First {
public String str = "second string";
public String value() {
return this.str;
}
public String str1 = value();
public static void main(String h[]){
First first = new Second();
System.out.println( "first.value() = " + first.value() );
System.out.println( "first.str = " + first.str );
System.out.println( "first.str1 = " + first.str1 );
}
}
Resolution of the member variable depends upon the type of the reference variable.
Resolution of member method (or instance method) depends upon the object (like new Second() in your example) pointed by the reference variable.
so first.str1 will resolve to a variable beloging to the class named First
Now the reason the first.str1 gives you null is because str1 (of class First) does not have a constant value. Infact it gets the value by executing the method value() which is an instance method belonging to class First. Since the instance of the class First is not created it cannot access this.str (return statement of that method) and hence first.str1 results into null
In your example though the type of the variable is of type First but the object its points to belongs to the class Second.
I hope, the above explaination solves your query ...
[ March 12, 2003: Message edited by: Manish Sachdev ]
[ March 12, 2003: Message edited by: Manish Sachdev ]
[ March 12, 2003: Message edited by: Manish Sachdev ]
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish, But how come it was able to print out "first string"?
Its a question...I'm not contesting, I would also like to know more. I'm not an expert and currently studying for the exam.
I might have an idea.
I agree the instance variable were resolved at compile time based on its reference. However, the str1 variable in First gets its value from an overridden method in second. An overridden method is always invoked in first (variable) object type not reference and since the Superclass' str1 variable gets its value from the value() overridden method, the superclass' has no way of getting to it?
** I'm not sure with this idea though.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish, after looking a second (maybe third) look at the code..I got your point. Preeti, disregard my previous post.
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dennis,
Hey we all r in the same boat...nobody's contesting.....me too preparing for JCP like you,
I also had a second look at the code and thought that when the call resolves to str1 of class First it tries to call value() method which in turn resloves to this.value() method call.
Since the class First is not initialized the variable gets the null value.
I am not so sure, but i think its this way.
But if i think in the other way, then my answer is wrong. For instance, say when you create an object of Second class, it calls the default constructor of class Second which inturn calls the default constructor of class First (Remember constructor chaining). So i think the object of the class First should be created.
Please throw some light on this.....
[ March 12, 2003: Message edited by: Manish Sachdev ]
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manish, thanks...I guess we need help here too as much as preeti does.
Anybody?
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ranchers....Please throw some light on this topic.
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys
let's take it step by step
now as u all know ,in inheritance base class constructor is called first and member variables are assign at time of cons. call!!
now let's take look at following codeclass First {
public String str = "first string";
public String value() {
return this.str;
}
public String str1 = value();
public First()
{
}
}
class Second extends First {
public String str = "second string";
public Second()
{
System.out.println("Hi I m Cons of Class Second ");
}
public String value() {
System.out.println("Hi I m Value of Class Second");
return this.str;
}
public String str1 = value();
public static void main(String h[]){
First first = new Second();
System.out.println( "first.value() = " + first.value() );
System.out.println( "first.str = " + first.str );
System.out.println( "first.str1 = " + first.str1 );
}
}
Hi I m Value of Class Second
Hi I m Value of Class Second
Hi I m Cons of Class Second
Hi I m Value of Class Second
first.value() = second string
first.str = first string
first.str1 = null
now in this code when cons. of class First
is called it try's to assign str1 by calling
Value of class Second as it is "overridden"
and Value is returning str of calss Second which is null at present because it's cons. is not called !!!
Hope this helps!! if not do bounce Back
regards
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're on the right track here, Manish - it has to do with initialization sequence. It's essential, in this case, to remember that parent classes are initialized prior to child classes.
In order to see what is happening, let's go through the initialization sequence.
1. First first = new Second();
This statement creates a new object of type Second. As Second extends First, we will initalize the First class first.
public String str = "first string";
Here, we create a new String variable and assign the value "first string" to it. Nothing shocking here.
public String str1 = value();
Here, we invoke the method value() in order to get the value for str1. However, remember that Java uses dynamic binding to determine which method to invoke. In this case, the object we're working on is of type Second (remember the declaration statement in main?). As we overrode the value() method in Second, that's the one that gets invoked. So, we go over to that method, which reads:
return this.str;
However, this.str refers to the variable str that resides in Second, not First. That variable hasn't been initialized yet! Therefore, the variable str1 is assigned the value null.
That's how we end up with a null in that variable. I hope that helps.
Corey
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey McGlone is perfectly correct. This is a very interesting question.
Thanks for posting this Preeti.
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all the replies.. The sequence of initialising and inheritance can be consfusing at time....am just trying to put several different combinations into a testing sequence and try to find the way the code works and understand it if possible
preeti
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Praful & Corey, u guys r amazing man....
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic