Friday, August 10, 2012

Java Packages


These are some notes from a java/c class I took 

Package-filesystem corresponding

  • Stay in the root when you compile (workspace for eclipse)
  • The package hierarchy corresponds to where it's stored in the filesystem.
  • For instance, mypackage.util is stored so mypackage java files are in the mypackage folder along with the util folder, but util java files are stored in util. 

Access-control

  • Package and class inheritance relationships affect access control
  • Java will only import what you use (from the import statement)
  • First it looks in the package for class reference, then looks in import statement
  • There are four possibilities:
    • same package, independent classes
    • same package, one is a subclass
    • different packages, independent classes
    • different packages, one is a subclass
  • Four different methods of access control
    • public- any class can access this
    • package (default) - can be applied to class (like public)-- only accessible by those in package
    • private-only the class that houses these methods and fields can see them
    • protected-classes within a package can see them; through inheritance of another package you can inherit protected members
    • static-members that can be accessed without declaring an object
    • order of most restrictive to least restrictive - private, package, protected, public
  • Packages
    • you cannot declare an object of a class from another package, unless the class you are trying to declare is public
    • if you do not declare the access for a field or method, it cannot be accessed from another package 


No comments:

Post a Comment