File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (Solved!) Inheritance, polymorphism and overriding. 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 "(Solved!) Inheritance, polymorphism and overriding." Watch "(Solved!) Inheritance, polymorphism and overriding." New topic

(Solved!) Inheritance, polymorphism and overriding.

Jem Hobstad

Joined: Oct 24, 2007
Posts: 5
Hey, hello.

I've just been taking the Whizlabs free mock exam. There's one snippet I can't explain ...

I supposed it would output:

Well actually it does not...
At compile time b1 is known as a SuperBase, but the instance method that is called at runtime is a Base instance... so... why does it output SuperBase?

That being said, I don't understand why the third line is "Base". Same problem I suppose.

Any help welcome ... thanks
[ October 24, 2007: Message edited by: Jem Hobstad ]

Maybe time for recreation of some kind? Check my entertainer "skills" ...<br /><a href="" target="_blank" rel="nofollow"></a>
Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38
Hi Jem,

I am not quite sure abut the reason,but as far as I concerned,here print method is overloaded not overriden because they have different argument list.As the method are overloaded,when you calling through the reference of super class,no hidding of methods occurs and they invoking the methods of super class.When invoking methods,references of SuperBase class is used.
That's why the out put is:

[ October 24, 2007: Message edited by: Saurabh Verma ]
Jem Hobstad

Joined: Oct 24, 2007
Posts: 5
Ah that makes sense!
You're right, it's about overloading - my mistake.

Checking the S&B book:
"Reference type determines which overlaoded method will be used at compile time".
-> at compile time, the only known print method is the one from SuperBase. Even if the real object that is referenced by b1 has a more accurate overlaoded method, it's the one visible at compile time that is used.

Thanks Saurabh!
I agree. Here's the link:
subject: (Solved!) Inheritance, polymorphism and overriding.
It's not a secret anymore!