File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why the overridden method is called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why the overridden method is called?" Watch "why the overridden method is called?" New topic
Author

why the overridden method is called?

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115


output:
Extension add(int v) i 2
Extension add(int v) i 6
Extension add(int v) i 22
22

In the above program, after line 1, line 2 should be called(add(int v)method of Base class), but instead line 3 is called(add(int v) of Extension class). i dont understand why line 3 is called instead of line 2?

SCJP 6.0 90%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

QuoteYourSources

That is called polymorphism. It is one of the basics of OO programming. It is however not always easy.
There is a lot of info about it on the web. You could start with the java tutorial.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Velu Kasirajah
Greenhorn

Joined: Feb 06, 2010
Posts: 15
It's due to method overriding (hiding). Since both the base and the extended classes have the same method, the one from the extended class will be executed.

Velu Kasirajah


Velu Kasirajah
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Thanks, i got it now. though it is very confusing sometimes.

This question is from Khalid A.Mughal(A programmer's guide to scjp certification) mock test.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Velu Kasirajah wrote:It's due to method overriding (hiding).

Overriding is not a Hiding. both are different things.
OverridingVsHiding
another Link
Vivek K Singh
Ranch Hand

Joined: Dec 22, 2009
Posts: 85

Well it is a case of Overriding (run-time polymorphism), I dont see method hiding anywhere. Method hiding will actually stop this type of behaviour.

When the execution starts, new Extension() is called that calls super constructor, but as the add(int) implementation has been oversiden so all add(int) calls will be made to the overriden add(int) method in the Extension class

so you get the output as:
<<initally int i is 0>>
then:

Extension add(int v) i 2 // Coz of the super constructor of Base class -> add() call
Extension add(int v) i 6 // Coz of the constructor of Extension class -> add() call
Extension add(int v) i 22 // Final call made from Qd073 Class
then the value is printed as 22.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why the overridden method is called?