• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this produce 0.2?

 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain this code why does this code produce 0,2.

Code
/*

class Sup{
int a = f();
public int f(){
return 1;
}
}
class Subclass extends Sup{
int b = 2;
public int f(){
return b;
}
}
class TestSub{
public static void main(String args[]){
Sup sup = new Subclass();
System.out.println(sup.a);
System.out.println(sup.f());
}
}
*/

[ EJFH: Add descriptive subject ]
[ July 16, 2007: Message edited by: Ernest Friedman-Hill ]
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to this good blog on Forward referencing
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops! forgot to add URL: Forward Referencing

Murali...
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Murali for the URL. I already read that but still in the dark about the code posted by AtulKumar Gaur. An explanation will be very helpful.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Al Mamun:
Thanks Murali for the URL. I already read that but still in the dark about the code posted by AtulKumar Gaur. An explanation will be very helpful.



The System.out.println(sup.a)--will call the int a in Sup class. When a class is initialized, initializations are executed in order from top to bottom, so a will hold a value of zero (default value for an integer variable), as f() will be called next.


System.out.println(sup.f())--will call the f() in Subclass.

Hope i'm clear


Gitesh
[ July 17, 2007: Message edited by: Gitesh R ]
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Gitesh:
The System.out.println(sup.a)--will call the int a in Sup class. When a class is initialized, initializations are executed in order from top to bottom, so a will hold a value of zero (default value for an integer variable), as f() will be called next.


I think the reason here a getting value of zero is:
when sub class is initialized, it tries to initialize a and hence calls f() method of sub-class (as this method is over-ridden). f() method of sub-class returns b which is not yet initialized at this point and hence returns zero to a.

Someone please correct me if I am wrong.

Try this example:


If we run above code we get value of i as 5 and NOT zero.

Murali...
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for 0,2 are because integer default value is zero.Since we didnt call the super class method which initialize the value for the super class instance variable.
2 is because it is Dynamic dispatch method which calls the sub class method.

Now i hope you got it
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I added the invisible super calls to the original code fromAtulKumar:



The first line of main Sup sup = new Subclass(); sets up a variable named sup of type Sup. sup refers to a new object created here, object of type Subclass, so the constructor of subclass is called.
The first thing it does is call the constructor of its super class (Sup).
Sup's constructor first calls Object's superclass (we don't see much of it) and then refers the return value of method f() to the variable a.
Calls of methods from the constructor are also polymorphic, so the f() method of Subclass (and not of Sup!) is called.
The method f() in Subclass returns b. Here is the problem, because we are still in the super class constructor. From the subclass constructor so far only the call to super has been done. So b still has its default value of zero, and this value will be returned from f() and then assigned to the variable a of the superclass.
The superclass's constructor is ready and we jump back to the sub class constructor, behind the call to super. There the value b is now updated to 2.
But the a in the super class is never updated, it is still 0.

Hence 0 2 as output.
All this text I wrote contains nothing that Ganesh hasn't already implied.


Yours,
Bu.
[ July 17, 2007: Message edited by: Burkhard Hassel ]
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understand why a contains zero.
Thank you all for those nice replies.
 
AtulKumar Gaur
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Burkhard Hassel
For such a good explaination.
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was a good question and very good explanation ! . I got my concept cleared .

Thanks Bu !

- Vivian
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent illustration Burkhard Hassel
 
geethal rodrigo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thing is when call sup.a, f() method in the sub class will work . I think you know variable hiding when took a super class reference and created a sub class object. Since sub class 'b' is hidden by the super class variable super class cannot see wthat the value is in the sub class.Only it know it'sinteger type. So it give the default value of integer. Just put an integer value(such as 'return 5;') to returning value for the sub class and surely it will print that given value.
[ July 19, 2007: Message edited by: geethal rodrigo ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic