GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes why to create two classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why to create two classes?" Watch "why to create two classes?" New topic
Author

why to create two classes?

armeen golden
Greenhorn

Joined: Oct 02, 2005
Posts: 8
in my text book, i have seen examples containing of two classes. one class contianing methods that perfom useful tasks and one containing method main, which creates an object of the other class and calls its method! for the life of me i can't understand why we would take such approach? thanks in advance.
Raymond Villeno
Ranch Hand

Joined: Jun 21, 2005
Posts: 63
in my text book, i have seen examples containing of two classes. one class contianing methods that perfom useful tasks and one containing method main, which creates an object of the other class and calls its method! for the life of me i can't understand why we would take such approach? thanks in advance.


Such approach is made simply because its more efficient and somewhat more straight forward, why you might ask.. because instead of recoding such method/s to every class that needs it why dont you just create a utility class with all the common methods that you need in all of your classes then just call it when you need it..


"Work Smart Not Hard"
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
It's a good programming practice.

Let's say the class with useful methods is mum class. In baby class mum will give birth to new baby. But you won't want to know how baby is created, or so to say the whole process inside mum class is hidden from baby class view.

Also you can always reuse the mum class. You can create different baby with type boy or girl. But you won't haveto rewrite new mum class everytime.


I'm not available, my BF's name is WORK.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Virtually all "real" Java programs are made up of not one, not two, but hundreds of classes. In such a program, each class is a small chunk that implements some well-defined functionality. Some classes are dedicated to GUI stuff. Others to network stuff. Others to event handling, or numerics, or data access... The more specialized each class is, the easier it is to understand it by itself. And when a program is made up of hundreds of classes, a class that can be understood and tested in isolation is your best friend.

So if your textbook is showing a simple class, and a main() routine in another class making use of that first class, the book is just trying to give you a small taste of this idea, that real programs are made up of many classes working together, each with a well-defined role.


[Jess in Action][AskingGoodQuestions]
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
I think all the repliers missed the main point here. In the case where two classes are being shown, the main reason for the distinction is to show for class creation and its use. Like Bruce Eckel demonstrates in his book, (dunno if any other author emphasizes this,) all programming activity can be divided into two parts:

a. Class Creation - done by a class creator
b. Class Usage - done by a client programmer

So, any programmer is always a class creator as well as a client programmer. In this example, one class is "created" and another demonstrates its use.


ASCII silly question, Get a silly ANSI.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Armeen"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
armeen golden
Greenhorn

Joined: Oct 02, 2005
Posts: 8
thank you every body. i have a much better grasp of why we do this. it makes a whole lot more sense now. thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why to create two classes?