• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Inheritance

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The options are :

A.9
B.18
C.20
D.21
E.22


This is from Whizlabs.
I had opted for D. ie 21
But after running the program , i get the result E ie.22
It seems the add() method of Base class was never executed , when i ws expecting it to execute once (the first time)

Can some one explain why the Extension add() is executed the first time.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the code is executing in the superclass constructor then the add method translates as this.add(1) since it is based on the current instance, and in the above case "this" is of type Exception and not Base.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin, i am also getting the value as 21.

here's how i got the value:
1) bogo(new Extension());-> invokes the constructor of Extension which invokes constructor of Base
Base() { add(1); } -> invokes add of base
void add(int v) { i += v; } -> i=0+1 (value of i is 1)

then return to constructor of Extension
Extension() { add(2); } -> invokes add of Extension
void add(int v) { i += v*2; --> i=1+2*2 (value of i is 5 now)

2) now execute bogo ()
bogo(new Extension())--> invokes with arg which a refernce of Extension
static void bogo(Base b) {
b.add(8); }
add() of Extension is invoked
i=i+v*2 i.e i=5+16= 21

can kindly let me know where i am going wrong in the code.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you run the program?

It gives the out put 22.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i also get the output as 22. Could anyone make clear of its order of execution with clear explanation
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why we get 22?

I think it's due to polymorphism but I'm not 100% sure.

My understanding is, when "bogo(new Extension())" is called, it will use the void add() that belongs to Extension class i.e. void add(int v) { i += v*2; } and it won't use the one in Base class which is void add(int v) { i += v; } because the Extension class method has overridden its parent/superclass method.

I hope that's the correct answer.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to run the program as well, and interestingly, the add(int v) method of Extension class is being called twice before the b.add(8) statement, first with the value of int v as 1 and second time int v = 2. I could understand why it was 2, but puzzled at how it called the Extension class add(int v = 1) at first pass.
Could anyone please clarify?
 
Resh Joseph
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Kevin has already given an answer for this. Sorry to bother.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please clarify this
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nabila,
Hope this will help

1) bogo(new Extension());-> invokes the constructor of Extension which invokes constructor of Base
Base() { add(1); } -> invokes add of Extension
void add(int v) { i += v*2 } -> i=1*2 (value of i is 2)

then return to constructor of Extension
Extension() { add(2); } -> invokes add of Extension
void add(int v) { i += v*2; --> i=2+2*2 (value of i is 6 now)

2) now execute bogo ()
bogo(new Extension())--> invokes bogo with ref of Extension
static void bogo(Base b) {
b.add(8); }
add() of Extension is invoked
i=i+v*2 i.e i=6+16= 22

Only thing here that was causing problem was in (1) which add() will be invoked. But that will invoke add() of Extension. It is Method overridding and Object Type determines which method to be invoked at runtime.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice explanation vidhya.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a very good question. Thanks for posting :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic