• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why to create two classes?

 
armeen golden
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
vivien siu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
armeen golden
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic