• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Reference variable casting

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if



at line 1 2 & 3 which methods are called means Dog's class And Animal's class???
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All those calls will invoke the method from the DOG class.

1.Because its overridden.
2.the object is actually a Dog class object and at this point the same object is actually refered by the Dog class reference
3.And again casting (for the time being)
[ November 14, 2008: Message edited by: James Tharakan ]
 
Chetan Dodiya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much....
[ November 14, 2008: Message edited by: Chetan dodiya ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember this rule, When a method is overridden(thru inheritance), then during run time, depending on the object type that the referrence variable is pointing to, the correspoding type's method will be invoked.

In the current example, ref variables a, d and the casted a(line 3) are all pointing to the Dog Object , doStuff() method is overridden and hence the doStuff() method in Dog Class will be invoked.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic