• 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

Jtips #39

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need help on this question too....Can you please explain how to approach this answer. thanx!!!
class Process {
byte b=127;

Process() {
this.methodA();
}

void methodA() {
System.out.println("Value of b is = " + b );
}

public static void main(String [] args) {
Processor p = new Processor();
}
}

class Processor extends Process {
byte b=126;

Processor() {
System.out.println("Value of b = " + b);
}

void methodA() {
System.out.println("Value of b = " + this.b);
}
}
Answer: prints value of b=0 and value of b is=126
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The approach is using order of initialization :
1. The storage allocated for the object is initialized to binary zero before anything else happens.
2. The Process-class constructors are called. At this point, the overridden methodA( ) method is called, which discovers b value of zero, due to step 1.
3. Member initializers are called in the order of declaration, b value become 126,
4. The body of the Processor-class constructor is called, and found the value of b is 126, due to step 3.

hope that helps
stevie
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stevie Kaligis:
The approach is using order of initialization :
1. The storage allocated for the object is initialized to binary zero before anything else happens.
2. The Process-class constructors are called. At this point, the overridden methodA( ) method is called, which discovers b value of zero, due to step 1.
3. Member initializers are called in the order of declaration, b value become 126,
4. The body of the Processor-class constructor is called, and found the value of b is 126, due to step 3.

hope that helps
stevie


Hi Stevie,
You wrote, "2. The Process-class constructors are called. At this point, the overridden methodA( ) method is called, which discovers b value of zero, due to step 1".
You did not mean "Process" and "overridden" method (i.e the method of superclass), did you ?
I believe, first the Processor's constructor is called. In this constructor, Process's constructor is then called via implicit super(). Inside Process's constructor, overriding methodA() (i.e the one belongs to the subclass is called).
Regards,
Lam

[This message has been edited by Lam Thai (edited April 26, 2001).]
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Lam...,
Implicitly, overriden means the one that belongs to the subclass,
where else can it be... ???
and obviously, my order initialization makes you confused, (didn't mean too), you are correct, i forgot to add the one you mention, it should be place before line 2.
thank's
stevie
 
Yuki Cho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch.
-Yuki
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I add a line in the program...
<code>
class Process {
byte b=127;
Process() {
System.out.println("Value of b is = " + b );
this.methodA();
}
void methodA() {
System.out.println("Value of b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Value of b = " + b);
}
void methodA() {
System.out.println("Value of b = " + this.b);
}
}
</code>
Answer:
value of b=127
value of b=0
value of b =126
according to you guys' explanation, the first b should be 0. Please tell me why. Thanks.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused by this question for a long time too. Then I figure out 2 points here:
1. Varaible are shadowed not over loaded.
2. subclass 's instance are instanitialized after call superclass's default constructor.
Correct me if i am worng.
 
Richard Huang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tracy Qi:
I am confused by this question for a long time too. Then I figure out 2 points here:
1. Varaible are shadowed not over loaded.


Hi Tracy Qi,
Can you tell me a little bit more about 'shadowed'? Thanks.
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Huang:
If I add a line in the program...
<code>
class Process {
byte b=127;
Process() {
System.out.println("Value of b is = " + b );
this.methodA();
}
void methodA() {
System.out.println("Value of b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Value of b = " + b);
}
void methodA() {
System.out.println("Value of b = " + this.b);
}
}
</code>
Answer:
value of b=127
value of b=0
value of b =126
according to you guys' explanation, the first b should be 0. Please tell me why. Thanks.


Hi Richard,
I don't know what you mean by "guys", but if you would like to know why you did not get 0 as the first answer then here is my 2 cents worth:
First, Java will set all class variables to their default values.
Second, 'main' will invoke the Processor's constructor. In this constructor, an implicit super() will be called. That means Process's constructor will be invoked.
Next, since Process is the top level class, its variable will be initialized (i.e the value of b in Process will then be set to 127). Then System.out.println() will be called to print 127.
After that overriding methodA() in subclass will be called. This will print the value of its own b, which still contains the default value of 0.
Once methodA() is done, the program is back in the Processor's constructor. At this time its variable b is initialized to 126.... And you know the rest.
So what happens is this:
#1. Alll class variables will be set with default values.
#2. Constructor of the subclass will be invoked.
#3. If there isn't any explicit super() provided immediately, an implicit super() will be called and the superclass constructor will be invoked. If the superclass is not the top level class, the process of invoking superclass constructor will be carried on until the top level class is reached.
#4. When the top level class is reached, its variables will then be initialized and then subsequent statemens in that constructor will be evaluated.
#5. When the top level class constructor is done, the stack is unwinded, and the program goes back to the previous constructor(s). Everytime, the process unwinds back to the previous constructor, its variables will be initialized... and the process continues to the original subclass.

Therefore, in your code if you let class Process extends from the following class
class A {
byte b = 100;
A() {
System.out.println("Value of b " = + b);
this.methodA();
}
void methodA() {
System.out.println("Value of b " + b );
}
}
}
then you will find that print out will be:
value of b = 100 - from top level class A
value of b = 0 - from overriding methodA() in Processor
value of b = 0 - from overriding methodA() in Processor
value of b = 126 - from constructor in Processor
Hope that helps,
Lam

[This message has been edited by Lam Thai (edited April 27, 2001).]
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tracy Qi:
I am confused by this question for a long time too. Then I figure out 2 points here:
1. Varaible are shadowed not over loaded.
2. subclass 's instance are instanitialized after call superclass's default constructor.
Correct me if i am worng.


Hi Tracy,
You are right!
The following link does address the difference between method overriding and variable shadowing/hiding. http://www.javaranch.com/ubb/Forum35/HTML/000247.html
Regards,
Lam
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to understand why the method of the subclass Processor
is called from the constructor of super class Process , even though
the call is made with the use of 'this'.

Process() //constructor of Process
{
this.methodA(); // this invokes subclass Processor method
}
1. According to me the methodA() of super class Process should be called.
Even if we do not use this , still the method of Processor class is called.
2. How can we call the methodA() of Process class from it's constructor.
Please explain the 1st question in detail.
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay....


1. According to me the methodA() of super class Process should be called.
Even if we do not use this , still the method of Processor class is called.
2. How can we call the methodA() of Process class from it's constructor.



  1. Here is the hint : it's late binding (polymorphism), couldn't give you more detail, you should refer to your book.
  2. Process process = new Process();

  3. stevie
 
vijay malhotra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stevie
This is in reference to my 1st question
I have read the late binding (polymorphism) from the book , but in this
case according to me the late binding does not takes place.
Late binding takes place when we assign a sub class object to
super class reference object. During the run time it's decided
to which sub class object the super class reference is pointing to.
On the basis of that appropriate calls to the method are made.
This would have taken place in this example if in the main method
we had
Process p = new Processor();
where Process is the super class and Processor is the sub class.
But in this example it's like this
Processor p = new Processor();
Then what is happening when we make a call in Process class constructor
this.methodA();
What this 'this' will do ?
//**//
Secondly if we have 2 variables for example int i = 100; in the
super class and int i = 200; in the subclass and if we want
to print the value of i from super class then i = 100 will
get printed , even though in the main method if we have
made the object like this
Processor p = new Processor();
See the code discussed in the previous posts.
Please explain.
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay...,
Q#1 :

By Vijay...
Late binding takes place when we assign a sub class object to super class reference object. During the run time it's decided to which sub class object the super class reference is pointing to. On the basis of that appropriate calls to the method are made. This would have taken place in this example if in the main method we had
Process p = new Processor();
where Process is the super class and Processor is the sub class.
But in this example it's like this
Processor p = new Processor();


and this is what Bruce Eckel "Thingking in Java" said, and I'm sure this represent what this is all about:

the dynamically bound call is resolved at run-time because the object cannot know whether it belongs to the class
that the method is in or some class derived from it. For Consistency, you might think this is what should happen inside constructors.
This is not exactly the case. If you call a dynamically bound method inside a constructor, the overridden definition for that method is used. However, the effect can be rather unexpected, and can conceal some difficult-to-find bugs.


By Vijay...
Then what is happening when we make a call in Process class constructor
this.methodA();
What this 'this' will do ?
//**//


Nothing..., it's just like :
class A {
int i;
A() {
...you can code i = 0, or this.i = 0;
}
}
Q#2 :

Secondly if we have 2 variables for example int i = 100; in the super class and int i = 200; in the subclass and if we want to print the value of i from super class then i = 100 will
get printed , even though in the main method if we have
made the object like this
Processor p = new Processor();
See the code discussed in the previous posts.


When a variable of an object is accessed using reference, it is the Type of the reference, not the class of the current object denoted by the reference, that determines which variable will actualy access.
stevie

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to clarify one point.
When the super class constructor is called implicitly
from any subclass, the class variables of the subclass are set to default values. So as in the example mentioned the call
to methodA prints the value of b as 0
Only after the call returns the variables of the subclass are initialized. So the value of b now becomes 126
Please correct me if wrong .

[ If possible can someone suggest me some link for class initialization. ]
 
vijay malhotra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stevie
This is in reference to your reply for question no 1 where
you gave reply from Bruce Eckel's "Thinking in Java".
If you are having the electronic version of the book
Please let me know the page number of the book where this behaviour is mentioned i.e the answer you gave to my question 1.
Thanks
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thinking in Java,
2 nd Edition, Release 11
chapter 7 : Polymorphism page 337
regard's
stevie
 
vijay malhotra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevie in helping me understand the new concept
about the Behavior of polymorphic methods inside constructors.
Please explain what does the author means by :
"A dynamically-bound method call, however, reaches �forward� or �outward� into the inheritance hierarchy. It calls a method in a derived class."
Why a dynamically bound method call have to reach forward in the
inheritence hierarchy ?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guys..i like to express my opinion on this Question;o~Q
Correct me if i am wrong..
superclass{}
subclass extends superclass{}
The order of initialization is like this:
Static variable/block(in superclass)==> static variable/block(in subclass)==>instance variable/block(in superclass)==>constructor(in superclass)==>instance variable/block(in sublcass)==>Finally the constructor in subeclass is executed..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic