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 Polymorphism and casting 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 "Polymorphism and casting" Watch "Polymorphism and casting" New topic

Polymorphism and casting

Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96

i was expecting this to output A as the casting will give an A object then print will be called on A object, but the output was B.


Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

It's simple. The call will be polymorphic. Just look at this modified code

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sammy Bill
Ranch Hand

Joined: Dec 29, 2008
Posts: 96
Got it Thanks. the casting was tricky though. i thougth the casting will make it A a = new A();

Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
It doesn't matter what type the variable refering to the object is; what matters is of what class the object actually is.
I agree. Here's the link:
subject: Polymorphism and casting
It's not a secret anymore!