• 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

Pleease Explain the Execution of the Code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ........



 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic