Java Inheritance woes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Casey Daniels
    New Member
    • Sep 2010
    • 5

    Java Inheritance woes

    Hello All,
    Still new to Java but got pass the messing around phase and am actually starting on a Project. What I'm trying to do is build a Application that will let me keep track of my flights (I'm a pilot). My background, I had a similar system in MS Access but i'm trying to get away from Microsoft all together. I am fairly knowledgeable in Database, which might be think of ideas that aren't really possible with Java, well atleast not without extensive coding.

    Anyways on to the question.

    What I itend to do is have a class called Aircraft_Type or such which would have a String for the name of the type and a double for the hourly pay rate for that type. and possbile other things about it, but thats all I can think of for the moment.

    Then I would have another class lets say called Aircraft which would I guess would be a subclass of Aircraft_Type which it would have an actual Aircraft Name and then it would have an Aircraft_type with it.

    So I would have two types of Aircraft

    Type A which pays 10.00 to fly
    and
    Type B which pays 12.50 to fly

    Then lets say I have
    Airplane_X which is Type A
    Airplane_Y which is Type A
    Airplane_W which is Type_B

    so when I create Airplane_X and tell its Type_A will it automactically fill attach all the descriptors of aircraft_type A to it? Or is that something that I would have to write in the constructor to search the Aircraft Types then copy them over. Which I would think would be cumbersome when writing to the File since all i would really want to write for each flight is that its Airplane_x not Airplane_x which is type_a which pays 10.00 etc.

    Thoughs on how to best do this?
    Casey
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    It sounds like an Aircraft instance HAS-An AircraftType rather than IS-An AircraftType. In that case the type would most natuarally be an instance variable of the aircraft (rather than being a parent class of it.)

    Code:
    class Aircraft {
        private AircraftType type;
        private String name;
    
        public Aircraft(String name, AircraftType type) {
            // etc
    }
    As for AircraftType: if there are a small number of these (each with their own name and hourly rate) you might want to consider an enum.

    Enum types in Oracle's Tutorial gives a couple of examples. The second (the Planet enum) is rather like your AircraftType in that there are a handful of different planets each having a mass and radius just as your aircraft types each have a name and cost.

    Comment

    • Casey Daniels
      New Member
      • Sep 2010
      • 5

      #3
      Thank you for pointing me in the right direction!!!

      Comment

      Working...