• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Overriding function problem

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
Take a look at this code:



What should the output be ? I thought it was 13.

According to my logic, in the main method, first an instance of Sub() is created. This calls the Sub() constructor. But since Sub extends Base, so the Base() constructor is called first.
Now the base function add() is called with argument 1 as add(1) and hence i gets the value 1.

The call returns back to Sub() where add(2) is called. Since i is already 1, so in Sub(), we get i += v*2 as 1 + 2*2 which is 5.

Finally the control comes back to bogo() where b.add(8) is called. Since the type of object passed is Base type, so the base class add is called here which gives i as i = 5 + 8 which is 13.

But the correct answer is 22.

Can anybody explain why my logic was wrong and what the correct logic is ?

Thanks alot
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have forgotten to take into acount the polymorphic aspects of the method calls. Put some print statements in the various methods to show yourself what really gets called.

Hint: 22 = 2 + 4 + 16;
[ September 10, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sub()'s add() method called, even from the Base() constructor. Presumably because the actual type of the object passed to Base b is Sub(). That's a good problem.
[ September 10, 2005: Message edited by: xxxx clark ]
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simpler example. Base obj has declared type Base and an
actual type of Derived.

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xxxx, you seem to have missed reading our JavaRanch Naming Policy. Please change your displayed name to comply with it. We need a displayed name in the form <first name><space><family name>, preferably your real name.
Thanks
-Barry

(NR)
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another, appropriate bit of code from a recent thread.
The output is 20 then 5.

A a = new B();
System.out.println(a.returnIt());
System.out.println(a.x);

 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another, appropriate bit of code from a recent thread.
The output is 20 then 5.

Given . . .

A a = new B();
System.out.println(a.returnIt());
System.out.println(a.x);

. . . you can see what's happening here.

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood very well the examples that Michael Clark showed.

But I still am confused regarding my code.
What Barry said is still unclear.

Could you explain stepwise (the way I explained my logic) how you arrived at the values of 2, 4 and 16 ?

It would be very kind of you
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherry wrote:

Finally the control comes back to bogo() where b.add(8) is called. Since the type of object passed is Base type, so the base class add is called here which gives i as i = 5 + 8 which is 13.



That is the misstep. The declared type of the object reference b is Base. The actual type of the object, however is
Sub. Actual type is the type that counts where polymorphism in Java is concerned. Base's add() method is never called.



DECLARED TYPE: Base
ACTUAL TYPE: Sub
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherry just remember one thing
that even if it is a constructor call - things work just as they do for overriding function ...

so first time add() method is called in Base()'s constructor - actually the Sub's add gets called since the object created is of Sub (So stop treating method calls from constructors in a special way!)

Thats the clue to the answer : 22

-
Shivani.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic