1 Reply Latest reply: Jan 3, 2011 6:32 AM by jduprez RSS

    Should I always use a supertype in my methods if I end up casting it?

    794383
      This is probably a simple question.

      I have a super class(Transport) and a more specific class (Car) which extends Transport.

      I have a method which uses a combination of the methods defined in Transport and Car.
      Currently I have written the method to take a 'Transport' as a parameter and then cast that to a Car - but I dont think theres any real point in doing that I think I should just write it to take a Car as a Parameter.

      Which was is correct?
        • 1. Re: Should I always use a supertype in my methods if I end up casting it?
          jduprez
          I have a method which uses a combination of the methods defined in Transport and Car.
          Note that depending on what it does (especially, if it doesn't require any state from any other entity), it might be located in class Car then.
          Currently I have written the method to take a 'Transport' as a parameter and then cast that to a Car - but I dont think theres any real point in doing that
          Indeed. In particular, what happens if the caller of the method passes a Transport instance that is not a car instance?
          I think I should just write it to take a Car as a Parameter.
          Probably. Or ... Think about locating the method in class car itself.
          But why have you specified the parameter to be a Transport in the first place? Are there other features, unstated so far, that would make the method apply to any kind of Transport?