Google
 

 

up previous next contents
Up: 2. Day 2: The Previous: 2.12 Exceptions Next: 3. Day 3: Standard   Contents

Subsections

2.13 Packages

2.13.1 Introduction

  • Packages have members that are related classes, interfaces, and subpackages.
  • Packages are useful for several reasons:
  • Packages create a grouping for related interfaces and classes.
  • Interfaces and classes in a package can use popular public names that make sense in one context but might conflict with the same name in another package.
  • Packages can have types and members that are available only within the package.

2.13.2 Package Naming

  • A package name should be unique.A good way to ensure unique package names is to use an Internet domain name (reversed):
        package COM.missiondata.utils;
    

2.13.3 Package Access

  • A public class or interface is accessible to code outside that package.
  • Types that are not public have package scope; they are available to all other code in the same package, but are hidden outside the package and even from code in nested packages.
  • Package scope is the default if public/protected/private are not declared.

2.13.4 Package Contents

  • Contain functionally-related classes and interfaces.
  • Classes in a package can freely access each other's non-private members.
  • Should provide logical groupings for programmers looking for useful interfaces and classes.
  • Packages can be nested inside other packages.

2.13.5 Examples

  • The Model/View/Controller paradigm can help illustrate a good way of packaging MVC-related classes for a fictitional Pizza Store project:
        com.alspizzastore.model.Pizza
        com.alspizzastore.model.Topping
    
        com.alspizzastore.view.OrderEntryScreen
        com.alspizzastore.view.ReceiptView
    
        com.alspizzastore.controller.ReceiptController
        com.alspizzastore.controller.PricingController