This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
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
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.
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.
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.