
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
238
Chapter 9
CHAPTER 9
Packages
Over the preceding few chapters, we’ve worked primarily with one or two classes
(and interfaces) at a time. But in larger applications, creating dozens of different
classes and grouping them logically together, perhaps with multiple interfaces, is
common. To group classes and interfaces together, we use packages. You may won-
der why we’d want to group classes and interfaces. To learn why, let’s consider a
hypothetical scenario.
Imagine you’re creating a game with a background music player. You create the
game in-house, but you use a third-party component for the music player. In the
game, you have a class for the player, named Player. Unbeknownst to you, the music
player component also defines a class named Player! When you attempt to use the
music player component, it doesn’t work because its version of the Player class is
ignored (your game’s Player class is defined first, and once a class is defined, it can-
not be redefined). This conflict is called a namespace collision or naming conflict.
What to do?
Well, you could rename your Player class to something like GamePlayer, but that
would require a lot of changes to your existing code. Besides, you like the name Player
and don’t really want to change it. And even if you did change your class’s name to ...