|
-
Nov 23rd, 2005, 01:10 PM
#1
Thread Starter
Hyperactive Member
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.
-
Nov 23rd, 2005, 02:08 PM
#2
Frenzied Member
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
}
-
Nov 23rd, 2005, 02:56 PM
#3
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.
-
Nov 23rd, 2005, 03:43 PM
#4
Thread Starter
Hyperactive Member
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?
-
Nov 23rd, 2005, 03:48 PM
#5
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.
-
Nov 23rd, 2005, 07:48 PM
#6
Thread Starter
Hyperactive Member
Re: OOP Class Question
That didn't really help any. ^_^ Thanks for the effort though.
-
Nov 24th, 2005, 05:10 AM
#7
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.
-
Nov 24th, 2005, 01:07 PM
#8
Thread Starter
Hyperactive Member
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?
-
Nov 24th, 2005, 06:01 PM
#9
Frenzied Member
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).
-
Nov 25th, 2005, 04:39 AM
#10
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.
-
Nov 25th, 2005, 01:22 PM
#11
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|