James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Inheritance Inheritance Reserved word protected Reserved word super
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.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks.
James Tam Object-Oriented Programming Relationships between classes: Inheritance Access modifiers: Public, private, protected Interfaces: Types Vs. Classes.
James Tam Classes And Objects Part III Relationships between classes: Inheritance Access modifiers: Public, private, protected Interfaces: Types Vs. Classes.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
James Tam Object-Oriented Principles in Java: Part III Inheritance Overloading methods Shadowing States A return to exceptions: creating new exceptions.
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Unit2: Object-oriented programming Getting started with Java Jin Sa.
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
1 CSE 331 Java Packages; JAR Archives slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Java: Chapter 1 Computer Systems Computer Programming II.
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.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Programming in Java CSCI-2220 Object Oriented Programming.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
2 Interfaces and Packages 3 What is an Interface? An interface defines a protocol of behavior that can be implemented by any class anywhere in the class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
ECE452/CS446/SE464 Design Patterns: Part I Questions A Tutorial by Peter Kim Based on slides prepared by Krzysztof Pietroszek.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Topics Instance variables, set and get methods Encapsulation
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
Computer Science 209 Software Development Packages.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Exception Handling
Chapter 16 UML Class Diagrams.
Packages When we name a program , we name it by class name…
Java Basics Packages.
An Introduction to Java – Part I
Packages Written part of final exam Alex Rudniy
Package & Java Access Specifiers
Packages, a method of subdividing a Java program and grouping classes
Java Programming with Multiple Classes
Joel Adams and Jeremy Frens Calvin College
Using java libraries CGS3416 spring 2019.
Chapter 7 Objects and Classes
Presentation transcript:

James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes

James Tam Decomposing Object-Oriented Programs Only By Classes Works well for small programs e.g., Trek GameStatus Galaxy StarShip CommandProcessor

James Tam Decomposing Larger Object-Oriented Programs There is another tool to group related classes, packages. Java.lang: classes that included with the ‘core’ portion of the Java language: String Math : Java.util.zip: classes that allow for the reading and writing to zip and gzip compressed files: ZipInputStream ZipOutputStream : Java.awt: the original collection of classes used for creating graphical user interfaces: Button Menu :

James Tam Benefits Of Employing Packages Increased ease finding a class Can be used to prevent naming conflicts An additional permission level (package level) may be set to allow certain classes to be instantiated only within the methods of the classes that belong to the same package java.lang Object String System ErrorExceptionStringBuffer Object org.omg.CORBA

James Tam Fully Qualified Names pack3.OpenFoo.toString() package name class namemethod name pack3.ClosedFoo.toString()

James Tam Importing Packages Importing all classes from a package Format import.*; Example import java.util.*; Importing a single class from a package Format import. ; Example import java.util.Vector;

James Tam Importing Packages (2) When you do not need an import statement: When you are using the classes in the java.lang package. You do not need an import statement in order to use classes which are part of the same package Excluding the import requires that the full name be provided: java.util.Random generator = new java.util.Random (); Vs. import java.util.Random; Random generator = new Random ();

James Tam Default Package If you do not use a package statement then the class implicitly becomes part of a default package All classes which reside in the same directory are part of the default package for that program.

James Tam Fully Qualified Names: Matches Directory Structure pack3.OpenFoo.toString() package name class name method name : home 233 examples packageExample pack3 OpenFoo.javaClosedFoo.java packages

James Tam Where To Match Classes To Packages 1.In directory structure: The classes that belong to a package must reside in the directory with the same name as the package (previous slide). 2.In the classes’ source code: At the top class definition you must indicate the package that the class belongs to. Format: package ; class { }

James Tam Matching Classes To Packages (2) Example package pack3; public class OpenFoo { : } package pack3; class ClosedFoo { : }

James Tam Matching Classes To Packages (2) Example package pack3; public class OpenFoo { : } package pack3; class ClosedFoo { : } Public access: Class can be instantiated by classes that aren’t a part of package pack3 Package access (default): Class can only be instantiated by classes that are a part of package pack3

James Tam Sun’s Naming Conventions For Packages Based on Internet domains (registered web addresses) e.g., com.tamj.games.productivity

James Tam Sun’s Naming Conventions For Packages Alternatively it could be based on your address e.g., ca.ucalgary.cpsc.tamj.games.productivity

James Tam Graphically Representing Packages In UML Package name +Classes visible outside the package -Classes not visible outside the package Package name +Class visible outside the package -Class not visible outside the package

James Tam Packages An Example The complete example can be found in the directory: /home/233/examples/packages/packageExample (But you should have guessed the path from the package name) packageExample pack1pack2pack3 Driver IntegerWrapper ClosedFooOpenFoo

James Tam Graphical Representation Of The Example (Unnamed) +Driver pack1 +IntegerWrapper pack2 +IntegerWrapper pack3 +OpenFoo -ClosedFoo

James Tam Package Example: The Driver Class import pack3.*; public class Driver { public static void main (String [] argv) { pack1.IntegerWrapper iw1 = new pack1.IntegerWrapper (); pack2.IntegerWrapper iw2 = new pack2.IntegerWrapper (); System.out.println(iw1); System.out.println(iw2); OpenFoo of = new OpenFoo (); System.out.println(of); of.manipulateFoo(); }

James Tam Package Example: Package Pack1, Class IntegerWrapper package pack1; public class IntegerWrapper { private int num; public IntegerWrapper () { num = (int) (Math.random() * 10); } public IntegerWrapper (int newValue) { num = newValue; } public void setNum (int newValue) { num = newValue; }

James Tam Package Example: Package Pack1, Class IntegerWrapper (2) public int getNum () { return num; } public String toString () { String s = new String (); s = s + num; return s; }

James Tam Package Example: Package Pack2, Class IntegerWrapper package pack2; public class IntegerWrapper { private int num; public IntegerWrapper () { num = (int) (Math.random() * 100); } public IntegerWrapper (int newValue) { num = newValue; } public void setNum (int newValue) { num = newValue; }

James Tam Package Example: Package Pack2, Class IntegerWrapper (2) public int getNum () { return num; } public String toString () { String s = new String (); s = s + num; return s; }

James Tam Package Example: Package Pack3, Class OpenFoo package pack3; public class OpenFoo { private boolean bool; public OpenFoo () { bool = true; } public void manipulateFoo () { ClosedFoo cf = new ClosedFoo (); System.out.println(cf); } public boolean getBool () { return bool; } public void setBool (boolean newValue) { bool = newValue; } public String toString () { String s = new String (); s = s + bool; return s; }

James Tam Package Example: Package Pack3, Class ClosedFoo package pack3; class ClosedFoo { private boolean bool; public ClosedFoo () { bool = false; } public boolean getBool () { return bool; } public void setBool (boolean newValue) { bool = newValue; } public String toString () { String s = new String (); s = s + bool; return s; }

James Tam Updated Levels Of Access Permissions: Attributes And Methods Private “-” Can only access the attribute/method in the methods of the class where it’s originally defined. Protected “#” Can access the attribute/method in the methods of the class where it’s originally defined or the subclasses of that class or in classes of the same package. Package - no UML symbol for this permission level Can access the attribute/method from the methods of the classes within the same package For Java: If the level of access is unspecified in a class definition this is the default level of access Public “+” Can access attribute/method anywhere in the program

James Tam Updated Levels Of Access Permissions Access level Accessible to Same classClass in same package Subclass in a different package Not a subclass, different package Public Yes Protected Yes No Package Yes No Private YesNo

James Tam Updated Levels Of Access Permissions Access level Accessible to Same classClass in same package Subclass in a different package Not a subclass, different package Public Yes: e.g., #1Yes: e.g., #5Yes: e.g., #9 Yes: e.g., #13 Protected Yes: e.g., #2Yes: e.g., #6 Yes: e.g., #10 No: e.g., #14 Package Yes: e.g., #3Yes: e.g., #7No: e.g., #11No: e.g., #15 Private Yes: e.g., #4No: e.g., #8No: e.g., #12No, e.g., #16

James Tam Levels Of Permission, Same Class Within the methods of the class, all attributes and methods may be accessed. public class ClassOne { public int num1; protected int num2; int num3; private int num4; public ClassOne () { num1 = 1; // Example #1 num2 = 2; // Example #2 num3 = 3; // Example #3 num4 = 4; // Example #4 }

James Tam Levels Of Permission, Class In Same Package package pack1; public class ClassOne { public int num1; protected int num2; int num3; private int num4; } package pack1; public class ClassTwo { private ClassOne c1; public ClassTwo () { c1 = new pack1.ClassOne (); c1.num1 = 1; // Example #5 c1.num2 = 2; // Example #6 c1.num3 = 3; // Example #7 // c1.num4 = 4; // Example #8 }

James Tam Levels Of Permission, Subclass In Different Package package pack1; public class ClassOne { public int num1; protected int num2; int num3; private int num4; } package pack2; import pack1.ClassOne; public class ClassThree extends ClassOne { private ClassOne c1; public ClassThree () { super.num1 = 1; // Example #9 super.num2 = 2; // Example #10 // super.num3 = 3; // Example #11 // super.num4 = 4; // Example #12 }

James Tam Levels Of Permission, Not A Subclass, Not In Same Package package pack1; public class ClassOne { public int num1; protected int num2; int num3; private int num4; } public class Driver { public static void main (String [] args) { pack1.ClassOne c1 = new pack1.ClassOne (); c1.num1 = 1; // Example #13 // c1.num2 = 2; // Example #14 // c1.num3 = 3; // Example #15 // c1.num4 = 4; // Example #16 }

James Tam You Should Now Know How packages work in Java How to utilize the code in pre-defined packages How to create your own packages How the 4 levels of access permission work in conjunction with classes in the same package, sub classes and classes that are neither in the same subclass nor in the same package.