Results 1 to 11 of 11

Thread: OOP Class Question

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question OOP Class Question

    When you code a class for an object, do you code it in the same file as the driver class or do you code it in a seperate file? I'm confused as to where to do this because my book didn't explain.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: OOP Class Question

    It's common practice to split them up. The driver class(in my opinion) should just instantiate and run:

    Code:
    public class Driver
    {
       public static void main(String[] arguments)
       {
            Car c = new Car();
            c.start();
            c.changeGears();
       }
    }
    Then the actual class:

    Code:
    public class Car
    {
      //methods
      //data
      //whatever
    }

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: OOP Class Question

    Does it matter which classes you place in which files? If you have a bunch of small related classes you could group them all in one file. If you have several large classes you may wish to place them in separate files.

  4. #4

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: OOP Class Question

    Yeah but if you split them up into seperate files, is there a different file type you save the class as and how do you import the class into the file that contains the driver class?
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: OOP Class Question

    I should never have started, I don't know the first thing about Java

    But, see if this helps you at all.

  6. #6

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: OOP Class Question

    That didn't really help any. ^_^ Thanks for the effort though.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: OOP Class Question

    Every publically visible class must be in its own file in Java. (Package-visible classes may be in those files too, but I'm not sure whether that's a good idea.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: OOP Class Question

    So I type in an object class in a seperate file and it's visible to all driver classes that I write?
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: OOP Class Question

    It's like this: If the 'object' classes are in the same directory, then you can simply use aggregation (creating an instance). If they are not in the same directoy, then you should probably have it in a com folder. I'd personally say save the packaged classes in the com folder for utility purposes.


    Say you have two classes: Dog and Cat. If they are in the same directory:


    Code:
    [  Directory  ]
        |        |
     Dog      Cat
    That kind of structure would allow you to create instances of each other (as long as they are not private).

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: OOP Class Question

    You can always create instances of other classes - you just need to use imports or fully qualified names.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Re: OOP Class Question

    Thanks you guys. I figured out how all that worked. Thanks a lot. My book didn't really explain how that really worked.
    Only those who try will become.

    Find me on identi.ca

    Twitter @gfmartin05

    Linux Wrap

Posting Permissions

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



Click Here to Expand Forum to Full Width