Newbie Question about Classes...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Newbie Question about Classes...

  1. #1
    Anthony Saffer Guest

    Newbie Question about Classes...


    Hi All,
    I've been programming in C++ for a few years and feel fairly comfortable
    there. But recently I switched over to Java because, quite frankly, it's
    easier. But I have a couple of questions about classes that I am hoping someone
    will take the time to answer. I would really appreciate it.

    1) I know that most serious Java programs use multiple class files. Why?
    Is this simply to prevent the existance of a huge, slow, and virtually unrunnable
    single class?

    2) If not, under what circumstances do I need to use multiple class files.
    For instance, I am writing a small telnet program in Java. If the user
    clicks "Connect" and hasn't entered any server information I will display
    a hand made dialog box telling him to go back and enter some information.
    Should I put the code for this dialog box in a seperate class file?

    3) How do I call methods in an external class file? Is it as simple as a
    class.method() call?

    I know these are really newbie questions and is probably wasting everyone's
    time but I can't seem to find any answers anywhere else. Again, thanks in
    advance for your answers.

    Anthony

  2. #2
    Paul Clapham Guest

    Re: Newbie Question about Classes...

    Why use many classes to build a program? Well, any non-trivial program
    breaks down into different parts - you need to do this, you need to do that,
    and so on. In object-oriented design, if you have a function or a group of
    similar functions, you "abstract" that function into a class. If your
    abstraction is correct (which for me is not usually on the first try), this
    class can then be used by other programs that you write in the future.
    Saves you programming time in the long run, but not in the short run.

    I don't think that a program consisting of a single class of say 2000 lines
    of code would run any slower than an object-oriented program consisting of
    40 or so classes; it would just be harder to maintain.

    Your example of the dialog box is a perfect example of where you would
    create a class. You will use many dialog boxes in your programming
    lifetime, and they will all be similar. Also, they may appear in many
    different contexts, whether it's telnet or accounts payable. So, create a
    dialog box class that can be used everywhere. (Incidentally, Java already
    has such a class. Check under AWT and Swing.)

    How do you call methods in another class? Well, you don't exactly do that,
    not usually. What you do is to create an object of the other class, and
    then you call its methods. If your other class is called MyDialogBox, first
    you create an object of that class:

    MyDialogBox gimmeData = new MyDialogBox("Please enter the name of the
    server.");

    and then you call the methods of the object you just created:

    gimmeData.display();

    There are several good books available on object-oriented programming and
    design available, it might be a good idea to have a look at some.

    Anthony Saffer <casaffer@rectec.net> wrote in message
    news:39caba26$1@news.devx.com...
    >
    > Hi All,
    > I've been programming in C++ for a few years and feel fairly comfortable
    > there. But recently I switched over to Java because, quite frankly, it's
    > easier. But I have a couple of questions about classes that I am hoping

    someone
    > will take the time to answer. I would really appreciate it.
    >
    > 1) I know that most serious Java programs use multiple class files. Why?
    > Is this simply to prevent the existance of a huge, slow, and virtually

    unrunnable
    > single class?
    >
    > 2) If not, under what circumstances do I need to use multiple class files.
    > For instance, I am writing a small telnet program in Java. If the user
    > clicks "Connect" and hasn't entered any server information I will display
    > a hand made dialog box telling him to go back and enter some information.
    > Should I put the code for this dialog box in a seperate class file?
    >
    > 3) How do I call methods in an external class file? Is it as simple as a
    > class.method() call?
    >
    > I know these are really newbie questions and is probably wasting

    everyone's
    > time but I can't seem to find any answers anywhere else. Again, thanks in
    > advance for your answers.
    >
    > Anthony




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center