This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pleease Explain the Execution of the Code

 
Sanjeev Gowda
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rachana Sharma
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic