This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 9291

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
Similar Threads
Will class casting creats new object on the heap?
Exam Question: I can't explain this downcasting
protected access
Overidden method from JQ5Plus exam