Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

160 HOUR 12:Making the Most of Existing Objects


When Java was first introduced, the system of sharing objects was largely
an informal one. Programmers developed their objects to be as independ-
ent as possible and protected them against misuse through the use of pri-
vate variables and public methods to read and write those variables.
Sharingobjects becomes more powerful when there’s a standard approach
to developing reusable objects.
The benefits of a standard include the following:

. There’s less need to document how an object works because anyone
who knows the standard already knows a lot about how it functions.
. You can design development tools that follow the standard, making
it possible to work more easily with these objects.
. Two objects that follow the standard are able to interact with each
other without special programming to make them compatible.


Storing Objects of the Same Class
in Vectors
An important decision to makewhen writing a computer program is
where to store data. In the first half of this book, you’ve found three useful
places to keep information: basic data types such as intand char, arrays,
and Stringobjects.
Any Java class can hold data. One of the most useful is Vector, a data
structure that holds objects of the same class.
Vectors are like arrays, which also hold elements of related data, but they
can grow or shrink in size at any time.
The Vectorclass belongs to the java.utilpackage of classes, one of the
most useful in the Java class library. An importstatement makes it avail-
able in your program:
importjava.util.Vector;

A vector holds objects that either belong to the same class or share the
same superclass. They are created by referencing two classes—the Vector
class and the class the vector holds.
Free download pdf