aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding function problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding function problem" Watch "Overriding function problem" New topic
Author

Overriding function problem

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
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

Joined: Sep 10, 2005
Posts: 48
Here's a simpler example. Base obj has declared type Base and an
actual type of Derived.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Sep 10, 2005
Posts: 48
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

Joined: Sep 10, 2005
Posts: 48
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

Joined: Jun 29, 2005
Posts: 128
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

Joined: Sep 10, 2005
Posts: 48
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
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
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.


/** Code speaks louder than words */
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding function problem