• 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

Clarification

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't understand the answer in a previous thread , so I am posting it as a new topic.(Please dont refer me to the old thread again)
What will be the result of the following code?
1. class Base {
2. int i;
3. Base(){
4. add(1);
5. }
6. void add(int v){
7. i+=v;
8.
9. }
10. void print(){
11. System.out.println(i);
12. }
13. }
14.
15. class Extension extends Base{
16. Extension(){
17. add(2);
18. }
19. void add(int v){
20. i+=v*2;
21.
22. }
23. }
24.
25. public class Test6{
26.
27. public static void main(String[] args){
28. bogo(new Extension());
29.
30. }
31. static void bogo(Base b){
32. b.add(8);
33. b.print();
34.
35. }
}
In the example seen above, if Base() is called when Extension is instantiated, then add(1) would result in 1, since
int i is not intialised in Base it has 0;
then add(2) would yield :1+2*2=5;
then b.add(8) would be 5+8*2=21.
Can anybody explain it to me. The answer when I compile is 22.
The explanation above was not clear to me how i=2;
Call me stupid but I need to know..
Thanks
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smithani,
I didn't see your new thread when I started composing reply for your previous post in the old thread. I replied it there. Please take a look at it here. Sorry for that.
regds
maha anna
[This message has been edited by maha anna (edited May 16, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic