This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman 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 Elasticsearch in Action this week in the Big Data 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: 9327

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.
Don't get me started about those stupid light bulbs.
subject: Polymorphism and casting