• 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

Polymorphism and HashMap problem

 
Greenhorn
Posts: 20
Mac OS X IntelliJ IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Mac OS X IntelliJ IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Mac OS X IntelliJ IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Mac OS X IntelliJ IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Mac OS X IntelliJ IDE MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can close this thread because I have found the problem.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't close threads in case someone else in the future has a similar problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic