Results 1 to 3 of 3
  1. #1
    ajfonty is offline Member
    Join Date
    May 2012
    Rep Power

    Default Creating a "Parking Garage" with cars as objects

    Hi everyone,

    I'm trying to create a "garage" filled with specific cars, ranging from simple Toyotas to fancy Aston Martins.

    When I create the cars as objects, though, I'm not sure which of the following to use:

    should I make an object Car, and use Car Toyota = new Car()? Or, should I make a separate object for each type of car, such as Toyota redcar = new Toyota()? Each car is going to have variables such as speed, number of passengers, etc.

    What are the benefits or drawbacks of using each type of object? I know it's a vague question, but I'm looking moreso for a theoretical answer than an actual block of code.

    Thanks in advance!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Creating a "Parking Garage" with cars as objects

    Suppose you want to distinguish three different top speeds and you als have cars that can have two, four or six passengers. If each were a different type then you'd have to deal with nine different car types (the Euclidean product). It's much easier to make the top speed and the number of passengers a property of a single Car class.

    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Art Vandelay is offline Member
    Join Date
    May 2012
    Rep Power

    Default Re: Creating a "Parking Garage" with cars as objects

    I would personally go with the first constructor: Car Toyota = new Car(). Thus, Car would be the top class...with variables common to all cars such as color, speed, number of passengers, etc. You could then do inheritance from the Car class... so perhaps class Toyota extends Car if there was something specific to the Toyota class...perhaps all Toyotas come with a special type of transmission, etc.

Similar Threads

  1. Replies: 3
    Last Post: 03-17-2012, 04:19 AM
  2. a parking garage program
    By jihad in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 03:06 PM
  3. Creating Java objects using "new"
    By linking90 in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 11:38 PM
  4. Replies: 0
    Last Post: 11-22-2008, 02:49 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts