Okay so I’ve been set an assignment from university and I just can’t get my head around how this problem, I’m the only person in the class that’s got this far and my lecturer’s aren’t getting back to me.
Basically it’s on inheritance and polymorphism, we are using a parent class and then creating the children from the parent;
private HashMap<String,Aircraft> allAircraft = new HashMap<String,Aircraft>();
Aircraft plane = new Plane(reg,pass,cargo);
So the parent cannot use the children methods, I understand the majority of the concept behind this and have managed to get my head around it. For one of the methods we have specifically been asked to use casting (even though I have been told countless times that if I have to use it then I should restructure) but for the other method we have been asked to call a method which is only specific of two of the children classes, casting won’t work here because I don’t know what the children classes are in the HashMap.
So my question is; how would I favor two children classes methods over the parent class whilst using polymorphism?
My mind is quite literally exploding.
If you need to downcast, then you’re not using polymorphism anymore. If that’s what the teacher wants, then you may still downcast in a safe way by testing that the actual type of the object is the appropriate one :
If this method is common to both subclasses, it might mean that they in fact share the same interface. So polymorphism could come back into play here :