Java Reference
In-Depth Information
Advantages of Using Objects
As we said at the outset, object-oriented programs are written using objects that are specific to the
problem being solved. Your pinball machine simulator may well define and use objects of type Table ,
Ball , Flipper , and Bumper . This has tremendous advantages, not only in terms of easing the
development process, but also in any future expansion of such a program. Java provides a whole range
of standard classes to help you in the development of your program, and you can develop your own
generic classes to provide a basis for developing programs that are of particular interest to you.
Because an object includes the methods that can operate on it as well as the data that defines it, programming
using objects is much less prone to error. Your object-oriented Java programs should be more robust than the
equivalent in a procedural programming language. Object-oriented programs take a little longer to design
than programs that do not use objects since you must take care in the design of the classes that you will need,
but the time required to write and test the code is sometimes substantially less than that for procedural
programs. Object-oriented programs are also much easier to maintain and extend.
Java Program Structure
Let's summarize the general nature of how a Java program is structured :
A Java program always consists of one or more classes.
You typically put the program code for each class in a separate file, and you must give each
file the same name as that of the class that is defined within it.
A Java source file must also have the extension .java .
Thus your file containing the class Hat will be called Hat.java and your file containing the class
BaseballPlayer must have the file name BaseballPlayer.java .
A typical program will consist of several files as illustrated in the following diagram.
This program clearly majors on apparel with four of the five classes representing clothing. Each source
file will contain a class definition, and all of the files that go to make up the program will be stored in
the same directory. The source files for your program will contain all the code that you wrote, but this is
not everything that is ultimately included in the program. There will also be code from the Java
standard class library , so let's take a peek at what that can do.
Search WWH ::




Custom Search