• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Q on variable Initialization

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

Output is = STP
My Query is , at //L1 we are creating a new Inastance of OuterTest with 'id' initialized with "STP". In //L2 we are creating a instance of InnerTest, which invokes the default constructor of 'OuterTest' within which 'id' is re-initialized to "Default". Then why is the output "STP".
help me understand whats going on i am
Reshma
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a trick question. The trick is the fact that the inner class extends the outer class. As a result, the inner class inherits the members of the outer class. Therefore, there are two variables named "id" associated with the inner class. One of the two is inherited. The second of the two is the one that is a member of the enclosing class. The "id" variable that is inherited has the "default" value. The "id" variable that is a member of the enclosing class has the value "STP".
[ March 27, 2003: Message edited by: Dan Chisholm ]
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton dan, your explanation was extremly helpful. If i do well in my SCJP, part of the credit goes to you. Thanks again for helping me understand JAVA better.
i still have a question to be answered, here it goes.......

Oput is 0.
My query is, in the Parent constructor when we say 'this.i'(//Line 1), are we not meaning to say the current objet's(that is 'c') variable 'i'.
I am very mush confused with the use of keyword 'this' in parent class, as to which resources do they point to. Can u please enlighten me on this.
Thanks
reshma
[ March 28, 2003: Message edited by: Reshma Pai ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding goes, when a subclass object is created even the superclass constructor is called but in this sequence the collection of variables for the two is different. That is to say when a superclass constructor gets called from within a subclass, a new object is created that holds the variables for the superclass.
So in the code snippet given by you, there will be two instances of variable i, and we are printing the variable i associated with the subclass whose value is not set. Hence the result 0.
Hope this helps. Please correct me if i am wrong.
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arvind,
I agree to what you say i,e. when in the code used 'c.i' its the value of i which is defined in 'C'. This is an example of data hiding.
My Query here is with the usage of keyword 'this' in parent class constructor. They behave differently with 'variables' & 'method invocation' ...
Considering this example...

output for the above code is = Child = 0
Is the usage of 'this' regarding 'variable' and 'methods' are different.

reshma
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reshma,
you have got it. By redefining the 'i' variable, the Child class is said to be 'hiding' the 'i' variable of the Parent class which it otherwise would inherit. Thus, references to 'i' of a Child instance always point to the 'i' variable of the Child class instead of the Parent class. In order to address the Parent's 'i' variable, you would have to insert the statement 'this.i = super.i;' into the Child class constructor.
The 'this' prefix in the Parent class therefore refers to an instance of the Parent class only, not to an instance of the Child subclass. Not even invoking the superclass constructor would have any effect on that.
And remember: The superclass constructor is not called automatically because the Child class is not instantiated by a default constructor, but by a no-arg constructor which is a different thing. In this case, in order to invoke the superclass constructor, the no-arg constructor must have the 'super();' statement at the beginning of its definition which is here completely empty.
Take care,
 
Falk Lucius
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reshma,
I have to correct myself, sorry. The superclass constructor is invoked automatically during the intantiation of a Child object. So, please erase the last paragraph of my previous reply.
Thanks,
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this case, in order to invoke the superclass constructor, the no-arg constructor must have the 'super();' statement at the beginning of its definition which is here completely empty.


This is not right.. refer the 8.6.5 section of JLS. I will paste below an abstract of what it mentions.


If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.


Hope this helps you
reshma
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Falk,
i had not seen your second post when i replied to your first post.
Reshma
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at the following codes, hope it will give you some hints.
class Parent{
int i=10;
Parent(){
this.showI();
}
public void showI(){
System.out.println("Parent = " + this.i);
}
}
class Child extends Parent{
int i;
Child(){
}
public void showI(){
//the following will access this Child object (pointed to by this)
System.out.println("Child = " + this.i);
//the following will access Parent object (pointed to by super)
System.out.println("Child = " + super.i);
}
public static void main(String[] args){
//the reason that the Child showI() is called in the Parent's Ctor
//is because showI() is dynamically bound.
//In Java, a static method is early bound, other instance methods are late binding.
//In other words, the object that invokes the method will depend on the real object type at the run time.
Child c = new Child(); //the real type for c at run time is type of Child
Parent d = new Child(); //same as above
Parent e = new Parent(); //will invoke showI() in the Parent
}
}
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for helping me understand the topic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic