how to specify which child class this object belong to after retrieving from a hashmap?

Posted by chandra wibowo on Stack Overflow See other posts from Stack Overflow or by chandra wibowo
Published on 2010-05-27T12:12:44Z Indexed on 2010/05/27 12:21 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

hi everyone,

i have a parent class called Course, and two child class PostgradCourse and UndergradCourse. i have a hashmap HashMap courses; i store all the postgradCourse and undergradCourse objects in the hashmap.

i want to retrieve an undergradCourse object from the hashmap using the key. Course course = courses.get(courseCode); then i want to call a method in the UndergradCourse class, setUnits() method course.setUnits(); but the compiler say cannot find symbol- method setUnit()

im pretty sure the problem is the compiler is looking for a method setUnit() in the Course class instead of UndergradCourse class

i did this but its not working UndergradCourse course = courses.get(courseCode); results in incompatible type

so how can i retrieve undergradCourse object from the hashmap as an undergradCourse object instead of course object? so then i can call a method inside the child class

thanks in advance

© Stack Overflow or respective owner

Related posts about class

Related posts about object