DOC PREVIEW
UMD CMSC 131 - Lecture 24: Packages

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 24: PackagesLast time:1.2-dimensional arrays2.Ragged Arrays3.Rectangular ArraysToday:1.PackagesCMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)1Java Program OrganizationProgram Organization:Java program: is composed of 1 or more Java source files. Source file: can have 1 or more class and/or interface declarations. (In our projects we have implemented one class/interface per file.)Public Class/Interface: If a class/interface is declared publicthe source file must use the same name.Only one public class/interface is allowed per source file. (Can you have non-public classes? We will discuss this later.)Packages: When a program is very large, its classes can be further organized hierarchically into packages.CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)2PackagesPackage: a collection of related classes and/or interfaces.Examples: The Java APIjavax.swing: classes dealing with the development of GUIs.java.lang: essential classes required by the Java language.java.text: facilities for formatting text output.java.util: classes for storing/accessing collections of objects.java.net: for network communication.Hierarchical: Packages can be divided into subpackages.java.awt: classes for basic GUI elements and graphics. java.awt.font: classes and interface relating to fonts. java.awt.geom: classes for defining 2-dimensional objects.There is no limit to the nesting depth.CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)3Access to Package MembersReview of Package Basics:Accessing Package Members:Fully qualified name: E.g., javax.swing.JOptionPaneImporting a single class:import javax.swing.JOptionPane;…JOptionPane.showMessageDialog( … );Importing all the classes:import javax.swing.*;…JOptionPane.showMessageDialog( … );Import semantics: import does not “insert” the Java files (as C/C++ do with “include” files). Instead, it tells the compiler where to look to find classes that the program refers to.Multiple import statements: You can have as many as you like. They go at the top of your .java file (before any classes or interfaces).java.lang: is automatically imported into every program.CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)4Defining your own packageWhy packages? Packages enable a programmer organize the code into smaller logically related units. A large program may consists of hundreds of classes. (Although we may not need to use them for the little projects in CMSC 131, but it is important to know how to create packages for when you will need them.)Every class is part of some package: Default package: If you do not specify a package a class becomes part of the "default package". Doesn’t require the package statement at the top of the .java file. What special privileges do packages provide? Classes defined within the same package can access one another more easily (without the need for importing or fully qualified names).CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)5Defining your own packageDefining a package: Add a “package” statement to specify the package containing the classes of this file.package mypackage;…public class myClass { … } // myClass is part of mypackageThis must be the first statement of your file. (Other than comments.)Subpackages: Packages organized into subpackages. This is specified using the notation “main.subpackage”. Example:package mypackage.mysubpackage;…public class myClass2 { … } // myClass2 is part of mysubpackage // … which is within mypackagePackages in Eclipse: File→→→→New→→→→Package. Enter the full name of the package (e.g. “mypackage” or “mypackage.mysubpackage”). Without Eclipse: Just insert this yourself (“package mypackage;”)CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)6Class Access and PackagesClass access within a package:Classes within a package can refer to each other without full qualification.If a class is not given an access specifier (public or private), it is assumed to have package access and it can only be accessed by other classes within the package.Class access across packages:A public class can be accessed from other packages.When this is done, either its name is fully qualified or is imported.We can view public classes of a package as the “interface” of the package with the outside world. (This is analogous to public methods of a class forming its interface.)Subpackages are not automatically imported:When you import a package (e.g. import java.awt.* ) it does not import the subpackages (e.g. java.awt.font must be explicitly imported).CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)7Package VisibilityPackage visibility is half-way between public and private.Package Visibility of classes:If a class is not declared public or private, it can be accessed by all and only the classes within the package. Package visibility of class members:If instance variables or instance methods are not declared public or private, they can be accessed by all and only the classes within the package.Package visibility is used in industry:A team working on a certain part of a project is likely to have its own package within which elements are “shared”among classes within the package. Used for cases where “shared” elements are not intended to be publicly visible (e.g., to other teams).CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)8ExampleTo illustrate these points, let’s consider an example of a simple hierarchical package, which we will create.Files:Driver.javaDriverFiles:Circle.javaRectangle.javaOtherShape.javaFiles:PublicClass1.javaPublicClass2.javaCircleRectangleOtherShapePublicClass1NonPublicClass1PublicClass2graphicsgraphics.shapesgraphics.otherstuffPackages: Files:Classes:CMSC 131 Fall 2007Jan Plane (adapted by Bonnie Dorr)9Example: graphics.shapes packagepackage graphics.shapes;public class Circle {private double radius;public String toString( ) { return "I'm a circle"; }}package graphics.shapes;public class Rectangle {private double height, width;public String toString( ) { return "I'm a rectangle"; }}package graphics.shapes;public class OtherShape {private Circle c;private Rectangle r;}File: Circle.javaFile: Rectangle.javaFile: OtherShape.javaNote: Classes of this package canbe accessed without the need forimport or using graphics.shapes.CircleCMSC 131


View Full Document

UMD CMSC 131 - Lecture 24: Packages

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 24: Packages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 24: Packages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 24: Packages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?