This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriden method - Output not as expected 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 "Overriden method - Output not as expected" Watch "Overriden method - Output not as expected" New topic
Author

Overriden method - Output not as expected

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi,

as far as I know the Object type determines which OVERRIDEN method will be used at runtime. So I tried out the code below:



So its a legal override and when I run the above code the output is "Super Ball"

I was expecting "Sub Ball" as the object type is Minor and the object type determines the overriden method

I thought I had these type of questions fully understood as I was getting the questions correct but this has really caught me out

Thanks


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
you didn't actually override the method since it is marked private in Major.

if you remove the private access modifier from your method within Major, you will get the results you expected.
[ November 17, 2008: Message edited by: Paul Campbell ]
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

private methods can not be overridden by subclasses
and inheritance not works with them
therefore object type is not important...


[ November 17, 2008: Message edited by: Anut Walidera ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Doh!

yes its a private method and is not overriden


So the reference type determines the call for private methods.

Thanks to all for pointing this out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriden method - Output not as expected