aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphism and casting" Watch "Polymorphism and casting" New topic
Author

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.

Why?

Thanks.
Oussama
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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();

Thanks.
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.
 
jQuery in Action, 2nd edition
 
subject: Polymorphism and casting