wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pleease Explain the Execution of the Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pleease Explain the Execution of the Code" Watch "Pleease Explain the Execution of the Code" New topic
Author

Pleease Explain the Execution of the Code

Sanjeev Gowda
Greenhorn

Joined: May 27, 2005
Posts: 5
//Here ia a piece of code according to me i should get 41 as the output but am getting 61 as the output So Please any one explain the code, the execution of the programme

class A
{
static int i=0;

A()
{
add();
}

void add()
{
i+=10;
}

static
{
i++;
}
}

class B extends A
{
B()
{
add();
}

void add()
{
super.add();
i+=20;
}
}

class testing
{
public static void main(String args[])
{
B b=new B();
System.out.println(b.i);
}
}

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

first off, when the B constructor gets invoked it will automatically call its super class constructor, the var 'i' will get initialized to 1 (by the static initializer block) then, then as i was say, the A constructor will get invoked, which calls add(), which increments i by 10 (i=11).

The constructor returns and add() in the B class will get called which has an explicit call to super.add() which calls the super class' add() method
(i = 21).

Now back to add() method in the B class, will increment 'i' by 20

i = 61

Cheers, Marzo.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I modified your code to put some System.out.println statements. Run the program and see if you can figure out the path of execution.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Rachana Sharma
Greenhorn

Joined: Jun 21, 2005
Posts: 15
hi,
when we invoke the derived class constructor first it calls the base class constructor as we all know,
but trying to call the method add() from the base class constructor will invoke the derived class add() method since it is overriding the behaviour of its base, had there not been any add() in derived certainly it would go the base class's add() method as expected.
hope this helps.
Dror Astricher
Ranch Hand

Joined: May 20, 2005
Posts: 31
Hi there

Marzo, 21 + 20 = 41 and not 61 but I think that your explanation is wrong.

1. first, variable i will get the value of 1 from the static method.
2. then, when we initialize B the A constructor will call the method add() in B as it is overriding the add() in A.
3. the add() in B will call add() in A (Super.add()) and i will be 11.
4. then method add() in B will continue and add 20 to i, so i is 31.
5. now it's time of the B constructor to evaluated. again we call to Super.add() and i will be 41. then continuing with adding 20 so i is 61.

Hope it helps
Have a great day
Dror
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
Hello,

I did not understand a couple of lines in the code mentioned above:

static {
i++;
}

Is it something like we can define all variables as static at once like:

static {
i++; j=10; etc...
}

which means that i, j and etc are all static variables?

Thanks,

Vijay
Ravi Shankar R
Greenhorn

Joined: Jan 05, 2005
Posts: 7
hi

here is the concept of overriding...... the method add()implemented by Class A is overridden by the Class B....... so both classes A & B when they are called use the same method add()declared in Class B i.e. the concept is always the parent class refers to the overidden method in the child class but the reverse is not true.......

Steps Of Execution...

1)When B b = new B(); is called the cursor moves to Class A where the default value of variable i changes from zero to 1 in the static block bcz static block is the first to executed when a class is loaded.

2)Now the cursor jumps to the Class B as it extends Class A the cursor again moves back to Class A constructor where the method add() is called. Now the cursor moves to Class B overriden method add()here the super.add()take the cursor back to Class A method add() where the value of i changes from 1 to 1+10 = 11 then cursor is again back to Class B overriden method add()where the value of i is changed from 11 to 11+20 = 31

3)Now the Constructor Class B is called so again the cursor moves back to Class B overriden method add() the cursor moves to Class A method add() where the value of i changes from 31 to 31+10 = 41 then cursor is again back to Class B overriden method add()where the value of i is changed from 41 to 41+20 = 61

4) Now the once the Class B has been instantiated the value of i is 61 thus the print statement would print the value as 61.


........ I hope this is clear ........



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pleease Explain the Execution of the Code
 
Similar Threads
contructor chain
what gets printed and why?
set of questions
order of execution
overriding vs hiding