wood burning stoves
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


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Doubt" Watch "Object Doubt" New topic
Author

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. a.foo(); 20. b.foo();

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

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, a.foo(), b.foo() // 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!!!
 
Have you checked out Aspose?
 
subject: Object Doubt
 
It's not a secret anymore!