how to specify which child class this object belong to after retrieving from a hashmap?
- by chandra wibowo
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