• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class casting

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two classes Employee and Manager. Employee is the parent class of Manager.
1)Manager m=new Manager();
2)Employee e=new Manager();
Point 1: m is a reference at manager object?
point 2: Class Employee has a Reference e at Manager object?
Is there two objects of Manager?One object has 'm' reference and other object has 'e' reference?
Thankz in advance
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an example of polymorphism. Using the superclass reference to refer to it's subclass. At runtime the superclass reference will actually be referirng to it's subclass "employee" Note that the superclass will be refering to it's own attributes, and not that of it's subclass. I am sure somone else may be able to give a better explanation.

Bosun
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I think that you have it right, but you kind of said it backwards.
What you have is two variables. One of TYPE Manager and one of type Employee.
The Manager variable m can only hold references to Manager objects. The Employee variable e can hold references to Employees OR to Managers because that is a "subset" or subclass of Employee.
In your example you used the new operator 2 times, therefore you created 2 objects. Both of them are Manager objects. You can tell because you invoked the Manager() constructor. One is referenced in a Manager variable and the other in an Employee variable.
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when would this be useful? If I need an Employee object, why instantiate a Manager object? What is the benefit of class casting/polymorphism?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see the usefulness better if you have more than one subclass with the same parent class.

For a good example, see shapes.jsp on this site.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic