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 ]
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.
MooN
Gitesh Ramchandani
Ranch Hand
Joined: Feb 28, 2007
Posts: 274
posted
0
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
Joined: Jul 11, 2007
Posts: 80
posted
0
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
Joined: Jul 02, 2007
Posts: 113
posted
0
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
Joined: Aug 25, 2006
Posts: 1274
posted
0
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.
Now I understand why a contains zero. Thank you all for those nice replies.
AtulKumar Gaur
Ranch Hand
Joined: Jun 24, 2007
Posts: 40
posted
0
Thanks Burkhard Hassel For such a good explaination.
Vivian Josh
Ranch Hand
Joined: Oct 31, 2006
Posts: 112
posted
0
This was a good question and very good explanation ! . I got my concept cleared .
Thanks Bu !
- Vivian
ahmed yehia
Ranch Hand
Joined: Apr 22, 2006
Posts: 424
posted
0
Excellent illustration Burkhard Hassel
geethal rodrigo
Greenhorn
Joined: Jul 18, 2007
Posts: 3
posted
0
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 ]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.