Java polymorphism - getting child command from parent map
Java polymorphism - getting child command from parent map
I've made two classes - Building
and Warehouse
. Warehouse
extends Building
. I've created Building
map (building_map
), where I've put both Building
objects and Warehouse
objects.
I'd like to get Warehouse
's method by using the map, but I can see only things that are from Building
class.
Building
Warehouse
Warehouse
Building
Building
building_map
Building
Warehouse
Warehouse
Building
building_map.get(ware_id); /*--Warehouse_function--*/;
(It's one of my first posts here so sorry if I've put too little informations on my problem)
EDIT:
Here's how I'm adding objects into the map:
public static Map<String, Building> building_map = new HashMap<>();
Building bD_02 = new Building("bD_02", 2, "Duargian", 2.5, 180, 450, 100, ""
+ "i0_024!i0_029!i0_036?90!10!10!" //upgrade lv 1
+ "&"
+ "" //upgrade lv 2
+ "&"
+ "" //upgrade lv 3
+ "&");
Building bD_03 = new Warehouse("bD_03", 3, "Duargian", 2.8, 800, 2500, 1500, 2.35, 3000, ""
+ "i0_021!i0_024!i0_029?200!500!70!" //upgrade lv 1
+ "&"
+ "" //upgrade lv 2
+ "&"
+ "" //upgrade lv 3
+ "&");
building_map.put("bD_02", bD_02);
building_map.put("bD_03", bD_03);
EDIT2:
Thanks, TungstenX! It worked, I can close the topic now.
Please add code of how your building_map
– TungstenX
Sep 13 '18 at 10:52
I've updated the question with code of building_map.
– FumyaHero
Sep 13 '18 at 10:57
you will have to check the object's type with
instanceof
and cast it to Warehouse
– Jack Flamp
Sep 13 '18 at 10:58
instanceof
Warehouse
See also the official tutorial to get started: docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
– Hulk
Sep 13 '18 at 11:02
3 Answers
3
You can cast the result of the get to a warehouse assuming that you extended Warehouse with Building. I.e class Warehouse extends Building
class Warehouse extends Building
And your map looks like:Map<String, Building> buildingMap = new HashMap<>();
Then you can do
Map<String, Building> buildingMap = new HashMap<>();
Update: Safe casting
Building building = buildingMap.get("A");
if (building instanceof Warehouse)
((Warehouse)building).warehousMethod();
what if the value of
building_map.get("key")
is a Building object?– Jack Flamp
Sep 13 '18 at 11:00
building_map.get("key")
Oh, it worked! Thank you for help, that's the thing I was looking for!
– FumyaHero
Sep 13 '18 at 11:01
@JackFlamp You are correct, I just gave an example ;-) Always check you objects
– TungstenX
Sep 13 '18 at 11:02
@FumyaHero Welcome to SO. Please select the solution/answer that suited you best.
– TungstenX
Sep 13 '18 at 11:04
Could you add safe casting to this post? Would be the cleanest.
– Wesley De Keirsmaeker
Sep 13 '18 at 11:47
If you a have a map of Building
s, not every object there has to be a Warehouse
. It can also be a Building
or other implementation.
Building
Warehouse
Building
If you're sure it's a warehouse, then you can cast it, like so:
Map<String, Building> map = new HashMap<>();
map.put("b", new Building());
map.put("w", new Warehouse());
Warehouse w = (Warehouse) map.get("w");
w.yourMethod();
but keep in mind that someone can put another instance of a child of Bulding
, like so
Bulding
class OtherClass extends Building
map.put("b", new Building());
map.put("w", new Warehouse());
map.put("o", new OtherClass());
Warehouse w = (Warehouse) map.get("b"); // ClassCastException
Warehouse w = (Warehouse) map.get("o"); // ClassCastException
PS: As @Przemyslaw Kruglej suggested, if you want to validate an implementation of a class in a generic collection (like map) in runtime you can use instanceof
before casting:
instanceof
Building possibleWarehouse = map.get("w");
if (possibleWarehouse instanceof Warehouse) // true
Warehouse warehouse = (Warehouse) possibleWarehouse;
+1, but it would be better if you provided some information about
instanceof
usage in this case to prevent the ClassCastException
.– Przemyslaw Kruglej
Sep 13 '18 at 11:58
instanceof
ClassCastException
@PrzemyslawKruglej Yes, I could but I didn't want to enourage such coding standards. It's better to design a better architecture, than to design it bad and then
instanceof
around it :) You're right though.– Danon
Sep 13 '18 at 11:59
instanceof
Danon sure I understand, however,
instanceof
is there if needed and even if it is better to use another design, it is still good to know there is something like instanceof
;)– Przemyslaw Kruglej
Sep 13 '18 at 12:03
instanceof
instanceof
Yes, I agree, that's right.
– Danon
Sep 13 '18 at 12:03
Values in map are Building type, so return type Object is expected to be building type. If as a developer you know that a specific key is binded to warehouse, than typecast it to warehouse. ((Warehouse) building_map.get("bD_03"))
take care of null also before typecasting.
– vermaji
Sep 13 '18 at 11:01
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please read about java naming conventions: no _ char in names (unless constants). This isnt python ;-) And please read Minimal, Complete, and Verifiable example and enhance your question accordingly. That example you give here is simply insufficient, it is not clear what you are asking for.
– GhostCat
Sep 13 '18 at 10:52