JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
You have a serial number (id) which belongs to a certain subclass of Thing. Lookup by the serial number that you pass in ...
Let's use polymorphism so we don't need to be worrying about what type of object we have. If each object knows how to output its own description, we should be able to call some output method which will know how to output. Then we aren't messing with maps, checking types, looking up codes, anything.... the JVM takes care of keeping track of who's who and who does what.
margaret gillon wrote:In OOP4 there are 3 different types of Things: Books, Videos, and Furniture. My nitpicker is saying that I don't need the IF sequence I wrote in the Lookup class to decide which object to call.
Hints appreciated.
margaret gillon wrote:I have referenced some other posts on Javaranch regarding this assignment and think I have a conceptual understanding of the fix but I don't know the syntax.
Here is where I am stuck..... How do I call that object's getDescription method ? As far as I can tell I cannot create a new object and load the hashmap object into it because I do not know what type of object the hashmap object is, I just know it's an object. Because there are multiple object types a call like the following cannot be done because the type of object is unknown
String(mystery object from hashmap) thingDesc = new hashmap.hashmap.get( serialNumber ).getDescription();
What is the syntax to access the object in the hashmap?
Syntax is not the issue here. Would inheritance and/or casting be useful in this situation? If you could find a way to call getDescription() on each object in your hashmap using Polymorphism, would you need to create a new hashmap? Try reading about Polymorphism again, and see if you can make the concept work for this situation. Think about solving your problem using inheritance.
TIA
If you could find a way to call getDescription() on each object in your hashmap using Polymorphism, would you need to create a new hashmap? Try reading about Polymorphism again, and see if you can make the concept work for this situation. Think about solving your problem using inheritance.
When you do things right, people won't be sure you've done anything at all.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |