Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism and HashMap problem

 
Brett Wiggins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Brett Wiggins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return B;


you might mean return b;

If yes then the above code is correct. Paste the complected code.
 
Brett Wiggins
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can close this thread because I have found the problem.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic