• 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

constructors calling constructors

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me that how this code is giving an output of 22?
Thanks

class base
{
int i;
base(){add(1);}

void add(int v){i = i + v;}
void print(){System.out.println(i);}
}
class sub extends base
{
sub(){add(2);}
void add(int v){i = i+v*2;}
void print(){System.out.println(i);}
}
public class yahoo
{
public static void main(String args[])
{
sub b = new sub();
bogo(b);
}
static void bogo(base b)
{b.add(8);
b.print();
}
}
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same question was asked and answered here. Take a look and if you still don't understand, let me know.
http://www.javaranch.com/ubb/Forum24/HTML/005725.html
Thanks,
Bill
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic