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 Object Doubt 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 "Object Doubt" Watch "Object Doubt" New topic

Object Doubt

Karthikeyan Pandian
Ranch Hand

Joined: Jun 09, 2013
Posts: 66
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19.; 20.;

// Please explain me the line 18 and 19, i am new to java.
Ganesh Jee

Joined: Jul 22, 2012
Posts: 14

line 18, Beta b = (Beta) a // It is required to cast as you can not assign a super class reference to a subclass reference directly
line 19,, // Here you making use of super class reference (which is pointing to sub class object), subclass reference( which intern pointing to sub class object) to invoke foo() method. So, both will invoke subclass method.
Karthikeyan Pandian
Ranch Hand

Joined: Jun 09, 2013
Posts: 66
Thank you Ganesh Jee... I understood clearly ..Cheers!!!
I agree. Here's the link:
subject: Object Doubt
It's not a secret anymore!