• 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

what is the output?

 
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the put put of it ..
i think 21 but the book says 22 ..
and in my netbeans 6.7 output give nothing only run:
22BUILD SUCCESSFUL (total time: 0 seconds)
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

Output is 22.

After the Base constructor the value of i is 2
After the Child constructor the value of i is 6
The bogo method then executes the Child method of add 6 + (8*2) = 22
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Or basically, inheritence applies, even before construction of the object is completed. The Child class' add() method gets called in every case, even when called from the Base class constructor.

Henry
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is '22'.

add method call from the base class constructor will invoke add method of the child class
 
appu sharma
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Or basically, inheritence applies, even before construction of the object is completed. The Child class' add() method gets called in every case, even when called from the Base class constructor.

Henry


thanks henry & Brij
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Or basically, inheritence applies, even before construction of the object is completed. The Child class' add() method gets called in every case, even when called from the Base class constructor.

Henry



Yea! The actual object is type of Child, so in the Base class constructor, Child class' version of add() method will be invoked!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinit sharma wrote:i think 21 but the book says 22 ..


Which book? We require that you QuoteYourSources when you copy and paste a question from a book, mock exam or other source.
 
appu sharma
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:

vinit sharma wrote:i think 21 but the book says 22 ..


Which book? We require that you QuoteYourSources when you copy and paste a question from a book, mock exam or other source.


its khalid Mughal 3rd edition page 964 bottom
 
Greenhorn
Posts: 25
Oracle PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have also got the result when you run your code.
See

22BUILD SUCCESSFUL (total time: 0 seconds)


See the start of above line.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Or basically, inheritence applies, even before construction of the object is completed. The Child class' add() method gets called in every case, even when called from the Base class constructor.


Both Base and Child are loaded before any code is run. This must be for the JVM to know
how to do the construction. So the Base/Child relationships are in place, including the
override of add(). Then the Child() constructor is called.

Jim ... ...
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic