Package versus Namespace

From CSSEMediaWiki
Revision as of 03:11, 25 November 2010 by WikiSysop (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A package and namespace are two strongly related concepts in OO languages. They differ only subtly, but this distinction provides an insight into an important distinction between two paradigms in OO languages.


A Namespace

A namespace is a generic term for a container and potentially an encapsulation boundary for the classes within them. In most languages, a namespace is programmatically defined for a block of text.

namespace a.name.space {

class { ... }

}

A Package

A package is Java specific language construct to define namespaces. Each file has a declaration of a the package that it is contained within. Each file may only be related to a single package.

package a.name.space;
...

By defining a whole file as part of a namespace and not having the ability to do otherwise pushes developers towards class encapsulation as the physical location of the class within the file system becomes more important than the blueprint of the object that a class provides.

See Also


Personal tools