Java Reference
In-Depth Information
Advantages of Using Objects
As I 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 , Flip-
per , and Bumper . This has tremendous advantages, not only in terms of easing the development process and
making the program code easier to understand, 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, program-
ming 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 because you must take care in the design of the classes that you
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.
Annotations
A Java source file can contain annotations . An annotation is not a Java language statement, but a special
statement that changes the way program statements are treated by the compiler or the libraries. You can
define your own annotations but most Java programmers never need to do so, so I'm not going into the how.
The reason for mentioning annotations is that you make use of a couple of annotations with some of the
examples in the topic.
To clarify the sort of thing an annotation does, I'm introducing you to one now. Earlier in this chapter you
compiled MyFirstApplet and got a warning message from the compiler. This warning had no value in the
context of this program, and you could have suppressed it by adding the following annotation immediately
preceding the MyFirstApplet class definition:
@SuppressWarnings("serial")
This annotation tells the compiler not to issue serial warning messages, where serial is the message
type. You could use this annotation to suppress any type of warning message, but obviously it is sensible to
only suppress warnings that really are irrelevant in a particular context.
Generic Classes
I have already mentioned generic classes so I had better explain a bit more about them. It often occurs that
you want to define classes for objects that store collections of things, people maybe, or temperatures, or
names perhaps. A class that stores a collection of people is likely to be very much the same as another class
that stores a collection of temperatures. The principle difference is the type of thing that is stored. This is
where a generic class type comes in. A generic class is a recipe for creating classes of a similar nature. You
can specify a generic class representing a collection of things that you can use to create a class that can store
a collection of things of a particular type. I explain generic classes in detail in Chapter 13 and you learn
about classes for storing collections of things in Chapter 14.
Search WWH ::




Custom Search