1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Programming in Java CSCI-2220 Object Oriented Programming.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Packages. Creating and Using Packages To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
Java Package Advantage of Java Package
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Topics Instance variables, set and get methods Encapsulation
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
1 Stack and Heap. 2 “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Definition: context – the.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
“Packages in Java”.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Software Construction
Inheritance-Basics.
Packages When we name a program , we name it by class name…
Classes, Libraries & Packages
Java Basics Packages.
Packages Written part of final exam Alex Rudniy
Java Programming Language
Packages and Interfaces
Package & Java Access Specifiers
Applying OO Concepts Using Java
PACKAGES.
Java Programming Language
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.

2 Lecture 2 What are Java Packages? Definition 1: A package is a collection of related classes and interfaces that provides access protection and namespace management. Definition 2: Java programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A type is accessible outside the package that declares it only if the type is declared public. The naming structure for packages is hierarchical. The members of a package are class and interface types, which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own. Package Members: A Java package can have members of either or both of the following kinds: Subpackages of the package. Types declared in the compilation units.

3 Lecture 2 What are Java Packages? (Cont’d) For example, in the standard Java Application Programming Interface (API):  The package java has subpackages: applet, io, lang, net, awt, and util, but no compilation units.  The package java.awt has a subpackage named image, as well as a number of compilation units containing declarations of class and interface types. If the fully qualified name of a package is P, and Q is a subpackage of P, then P.Q is the fully qualified name of the subpackage. For the subpackages of package java named lang, util, and io, the fully qualified package names are therefore java.lang, java.util, and java.io.

4 Lecture 2 What do we need Java Packages? Java programmers bundle groups of related classes into packages: To make classes easier to find and use. To avoid naming conflicts. To control access.

5 Lecture 2 Packages and Subpackages A package may not contain a type declaration and a subpackage of the same name, or a compile-time error results. Here are some examples: 1.Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image. 2.If there is a package named mouse and a type Button in that package (which then might be referred to as mouse.Button), then there cannot be any package with the fully qualified name mouse.Button or mouse.Button.Click. 3.If COM.Sun.java.jag is the fully qualified name of a type, then there cannot be any package whose fully qualified name is either COM.Sun.java.jag or COM.Sun.java.jag.scrabble.

6 Lecture 2 Packages and Subpackages (Cont’d) The java package includes several other sub-packages, such as java.io, which provides facilities for input/output, java.net, which deals with network communication, and java.applet, which implements the basic functionality of applets. The most basic package is called java.lang, which includes fundamental classes such as String and Math. It might be helpful to look at a graphical representation of the levels of nesting in the java package, its sub-packages, the classes in those subpackages, and the methods in those classes. This is not a complete picture, since it shows only a few of the many items in each element: Methods

7 Lecture 2 Compilation Units A compilation unit consists of three parts, each of which is optional: A package declaration, giving the fully qualified name of the package to which the compilation unit belongs. import declarations that allow types from other packages to be referred to using their simple names. Type declarations of class and interface types.

8 Lecture 2 How to create a Java Package? As mentioned earlier, the classes and interfaces that are part of the JDK are members of various packages that bundle classes by function: applet classes are in java.applet, I/O classes are in java.io, and the GUI widget classes are in java.awt. You can put your own classes and interfaces in packages, too. Let's look at a set of classes and examine why you might want to put them in a package. Suppose you write a group of classes that represent a collection of graphics objects such as circles, rectangles, lines, and points. You also write an interface Draggable that classes implement if they can be dragged with the mouse by the user.

9 Lecture 2 How to create a Java Package? An Example public class Point { int x_coord; int y_coord; public Point() { x_coord = 0; y_coord = 0; } // End of Point Constructor 1 public Point(int x, int y) { x_coord = x; y_coord = y; } // End of Point Constructor 2 } // End of class Point Point.java public class Rectangle extends Point { double width; double height; public Rectangle(int x, int y, double h, double w ) { super(x, y); width = w; height = h; } // End of Rectangle Constructor } // End of class Rectangle Rectangle.java public class Square extends Point { double edge; public Square(int x, int y, double e) { super(x, y); edge = e; } // End of Square Constructor } // End of class Square Square.java public class Circle extends Point { double radius; public Circle(int x, int y, double r) { super(x, y); radius = r; } // End of Circle Constructor } // End of class Circle Circle.java public interface Draggable { } // End of interface Draggable Draggable.java

10 Lecture 2 How to create a Java Package? An Example (Cont’d) Now, we would like to to put these classes and the interface together in a package, for several reasons: You and other programmers can easily determine that these classes and interfaces are related. You and other programmers know where to find classes and interfaces that provide graphics-related functions. The names of your classes won't conflict with class names in other packages because the package creates a new namespace. You can allow classes within the package to have unrestricted access to each other, yet still restrict access for classes outside the package.

11 Lecture 2 Creating a Java Package Now, you can easily create your own package, say geometry, and put all the above Java class and interface definitions in it. Simply, add the package statement at the to of all the previous Java files as follows: package geometry; public class Point { int x_coord; int y_coord; public Point() { x_coord = 0; y_coord = 0; } // End of Point Constructor 1 public Point(int x, int y) { x_coord = x; y_coord = y; } // End of Point Constructor 2 } // End of class Point Point.java package geometry; public class Rectangle extends Point { double width; double height; public Rectangle(int x, int y, double h, double w ) { super(x, y); width = w; height = h; } // End of Rectangle Constructor } // End of class Rectangle Rectangle.java Package geometry; public interface Draggable { } // End of interface Draggable Draggable.java Java Package Statement

12 Lecture 2 Creating a Java Package (Cont’d) You include the package statement in all the Java files you intend to include in your package. Java Package Statement package geometry; public class Circle extends Point { double radius; public Circle(int x, int y, double r) { super(x, y); radius = r; } // End of Circle Constructor } // End of class Circle Circle.java package geometry; public class Square extends Point { double edge; public Square(int x, int y, double e) { super(x, y); edge = e; } // End of Square Constructor } // End of class Square Square.java If you do not use a package statement, your class or interface ends up in the default package, which is a package that has no name. Generally speaking, the default package is only for small or temporary applications or when you are just beginning development. Otherwise, it is recommended to have classes and interfaces belonging to named packages.

13 Lecture 2 Package Naming With programmers all over the world writing Java classes and interfaces, it is conceivable and even likely that two programmers will use the same name for two different classes. In fact, the previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. Yet, the compiler allows both classes to have the same name because they are in different packages and the actual name of each class includes the package name. That is, the name of the Rectangle class in the geometry package is really geometry.Rectangle, and the name of the Rectangle class in the java.awt package is really java.awt.Rectangle. This works just fine unless two independent programmers use the same name for their packages. In this case, what prevents to programmers to have the same name for their own packages? By simply following the Java convention (to be presented in the Lab).

14 Lecture 2 Java Packages and Host File System As an extremely simple example, all the packages and source and binary code on a system might be stored in a single directory and its subdirectories. Each immediate subdirectory of this directory would represent a top level package, that is, one whose fully qualified name consists of a single simple name. The directory might contain the following immediate subdirectories: C Drive windows java msoffice mypack Continuing the example, the directory java would contain, among others, the subdirectories shown below. These subdirectories correspond to the packages java.applet, java.awt, java.io, java.lang, and java.net that are defined as part of the Java API. java applet awt io net lang

15 Lecture 2 Java Packages and Host File System (Cont’d) The subdirectory mypacks may contain your own packages that you have created for your personal use and to share with other Java programmers. mypacks geometry physics science Still continuing the example, if we were to look inside the subdirectory geometry,we might see the following files: Circle.javaCircle.class Draggable.javaDraggable.class Point.javaPoint.class Rectangle.javaRectangle.class Square.javaSquare.class

16 Lecture 2 Using Package Members Only public package members are accessible outside the package in which they are defined. To use a public package member from outside its package, you must either: Refer to the member by its long (disambiguated) name. Import the package member. Import the member's entire package.

17 Lecture 2 Referring to Package Members by Names You can use a package member's short name if the code you are writing is in the same package as that member or if the member's package has been imported. However, if you are trying to use a member from a different package and that package has not been imported, then you must use the fully qualified name of the member, which includes the package name. This is the fully qualified name for the Rectangle class declared in the geometry package in the previous slides: geometry.Rectangle You could use this long name to create an instance of geometry.Rectangle : geometry.Rectangle myRect = new geometry.Rectangle(0, 0, 20, 12); You'll find that using long names is okay for one-shot uses. But you'd likely get annoyed if you had to write graphics.Rectangle again and again. Also, your code would get very messy and difficult to read. In such cases, you can just import the member instead.

18 Lecture 2 Importing a Package Member To import a specific member into the current file, put an import statement at the beginning of your file before any class or interface definitions (but after the package statement, if there is one). Here's how you would import the Circle class from the geometry package created in the previous slides: import geometry.Circle; Now you can refer to the Circle class by its short name: Circle myCircle = new Circle(0, 0, 15); This approach works just fine if you use just a few members from the geometry package. But if you use many classes and interfaces from a package, you really just want to import the whole package and forget about it.

19 Lecture 2 Importing an Entire Package To import all of the classes and interfaces contained in a particular package, use the import statement with the asterisk * wildcard character: import geometry.*; Now you can refer to any class or interface in the geometry package by its short name: Circle myCircle = new Circle(0, 0, 15); Rectangle myRect = new Rectangle(0, 0, 20, 12); The asterisk in the import statement can be used only to specify all of the classes within a package, as shown here. It cannot be used to match a subset of the classes in a package. For example, the following does not match all of the classes in the geometry package that begin with "A": import geometry.A*; // does not work Instead, it generates a compiler error. With the import statement, you can import only single package member or an entire package.

20 Lecture 2 Disambiguating a Name If by some chance a member in one package shares the same name with a member in another package and both packages are imported, you must refer to the member by its long name. For example, the previous example defined a class named Rectangle in the geometry package. The java.awt package also contains a Rectangle class. If both geometry and java.awt have been imported, then the following is ambiguous: Rectangle rect; In such a situation, you have to be more specific and indicate exactly which Rectangle class you want by using the member's long name: geometry.Rectangle rect; or: java.awt.Rectangle rect;

21 Lecture 2 Automatically Imported Packages It is should be noted that the Java runtime system automatically imports three packages for you: 1.The default package (the package with no name). 2.The java.lang package. 3.The current package.

22 Lecture 2 Named and Unnamed Packages Named Packages: A package declaration in a compilation unit specifies the name of the package to which the compilation unit belongs. Here is an example of named package declaration: package ics.ics201.Shapes; class Point { int x, y; } Thus, the fully qualified name of class Point is: ics.ics201.Shapes.Point. Unnamed Packages: A compilation unit that has no package declaration is part of an unnamed package. As an example, the compilation unit: class FirstCall { public static void main(String[] args) { System.out.println("Mr. Watson, come here. I want you."); } } defines a very simple compilation unit as part of an unnamed package.