aspose file tools*
The moose likes Java in General and the fly likes Polymorphism and HashMap problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Polymorphism and HashMap problem" Watch "Polymorphism and HashMap problem" New topic
Author

Polymorphism and HashMap problem

Brett Wiggins
Greenhorn

Joined: Mar 23, 2009
Posts: 19
Hi everyone,

I have a problem with some code I am working on. I have a superclass A and subclasses B and C. I have a HashMap;



I can add objects of type B and C to the map just fine;



but when I use;



I get an object of type A and when I try and call methods in the B and C classes I get error messages;



help would be cool,

Brett
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Even if the actual type is B the compiler doesn't know. If you're 100% certain that the object really is a B (e.g. by using instanceof) you need to cast:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brett Wiggins
Greenhorn

Joined: Mar 23, 2009
Posts: 19
Hi Rob,

Thanks for the help. I am using the code you posted in a method that searches and retrieves values from a HashMap. The return type of the method is of type A but the HashMap stores only values of type B and C. So when I go and use



The compiler still thinks it's an A. Here is a code example



As I said before the return type of the method is of type A but I need the B and C objects for the program to work.
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
return B;


you might mean return b;

If yes then the above code is correct. Paste the complected code.
Brett Wiggins
Greenhorn

Joined: Mar 23, 2009
Posts: 19
here is the code I am having problems with;



The method returns a Student which shouldn't be a problem. But when I use the object returned by this method I can only use methods from the Student class and not methods from the Undergraduate and Postgraduate classes.

cheers,

Brett.
Brett Wiggins
Greenhorn

Joined: Mar 23, 2009
Posts: 19
here is the original code



when I try and use the object returned by the method I can only use methods from the student class instead of it's subclasses. I am putting objects of type Undergraduate and Postgraduate in the HashMap. I cannot see this to be a problem even though the HashMap is declared HashMap<String,Student>, As I understand it, poloymorphism should let me do this.
Brett Wiggins
Greenhorn

Joined: Mar 23, 2009
Posts: 19
You can close this thread because I have found the problem.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

We don't close threads in case someone else in the future has a similar problem.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Polymorphism and HashMap problem