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.

Slides:



Advertisements
Similar presentations
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
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.
Packages and Class Visibility The objectives of this chapter are: To understand logical class groupings using packages To explain the environment variable.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
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.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
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
UML Basics & Access Modifier
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Writing Classes (Chapter 4)
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.
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.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Programming in Java CSCI-2220 Object Oriented Programming.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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”
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces and Inner Classes
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Classes, Interfaces and Packages
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.
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.
Java Package Advantage of Java Package
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 2
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
External Scope CECS 277 Mimi Opkins.
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…
Java Basics Packages.
Interfaces.
Packages Written part of final exam Alex Rudniy
Packages and Interfaces
Package & Java Access Specifiers
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Interfaces,Packages and Threads
Presentation transcript:

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 Modifiers

Unit 052 What is a Package? A package is a collection of related classes and interfaces that provides access protection and namespace management. The classes in Java API are organized into packages. For example: Applet classes are in java.applet package I/O classes are in java.io package GUI classes are in java.awt. You can also organize the classes you create into packages and you should. Why?

Unit 053 Why use Packages? Easy to know where to find classes and interfaces that provide a particular set of functions. –For I/O functions go to java.io package. Avoid conflict with class names in other packages. Classes in different packages can have same name. –A package provides a new namespace. You can allow classes within a package to have access to each other.

Unit 054 Creating a Package To create a package, put a package statement at the top of each source file for the class or interface that belongs to the package. For example, to create a package, graphics, to store the classes: Shape, Circle, Rectangle, and Square, we should add the statement, package graphics; as the first statement in each of the source files. 1.package graphics; 2.public abstract class Shape{ 3.public String name(){ 4.return getClass().getName(); 5.} 6.public abstract double area(); 7.public abstract perimter(); 8.public String toString(){ 9.return name()+"\nPerimeter: "+perimeter()+"\nArea: "+area(); 10.} 11.}

Unit 055 Creating a Package-Cont’d The scope of the package statement is the entire source file, so if source file contains more than one class or interface, they all belong to the package specified by the package statement. If package is not specified for a class or interface, then the class or interface belongs to the default package, which is a package that has no name.

Unit 056 Naming a Package By convention, packages are named using lower case letters. Dots are used to group related packages together. For example, the packages: java.awt, java.io, java.lang, are named as such to indicate that they all belong to Java API. Similarly, java.awt.color, java.awt.geom, java.awt.event, are named as such to indicate that they all belong to a group of classes for programming GUI applications. Notice that this grouping is for the human reader only. As far as the system is concerned, the packages java.awt.color is completely independent of java.awt.geom.

Unit 057 Naming a Package-Cont’d Within one organization, it is clear that packages can be used to avoid conflict in class names. What about between different organizations? It is likely that two organizations working on similar projects may come up with the same package names and probably common class names. Thus, a user dealing with these two organizations may face conflict. To avoid this, by convention, organizations use their reversed Internet domain name in their package names, like: com.company.package We shall name our packages in this course starting with the prefix, ics201. Example: ics201.graphics, ics201.lab01, etc.

Unit 058 Using Package Members To use a public package member (a class or interface) from outside its package, you must do one of the following: oRefer to the member by its long name oImport the package member, or oImport the member's entire package. Each of these methods is appropriate for different situations, as explained in the following sections.

Unit 059 Referring to a Package Member by Full Name So far, our examples have referred to classes and interfaces by their simple names, such as Rectangle. You can use a package member’s simple name if: –The code you are writing is in the same package as the member –If the member’s package has been imported. However, if a member is from a different package and that package has not been imported, you must use the member’s fully qualified name: ics201.graphics.Rectangle To create an instance of the member we use: ics201.graphics.Rectangle myRect = new ics201.graphics.Rectangle();

Unit 0510 Importing a Package Member To import a specific package member into the current file, put an import statement at the beginning of the file before any class or interface definitions but after the package statement ( if there is one). 1.package ics201.lab02; 2.import ics201.graphics.Shape; 3.import ics201.graphics.Rectangle; 4.import ics201.graphics.Square; 5.import ics201.graphics.Circle; 6.public class TestShapes{ 7.public static void main(String[] args){ 8.Shape[] shapes = new Shape[2]; 9.shape[0] = new Rectangle(5, 10); 10.shape[0] = new Square(10); for(int i = 0; i<shape.length; i++) 13.System.out.println(shape[i]); 14.} 15.} This approach is used if few members are needed from a package.

Unit 0511 Importing an Entire Package To import all of the classes and interfaces contained in a particular package, use the asterisk (*). 1.package ics201.lab02; 2.import ics201.graphics.*; 3.public class TestShapes{ 4.public static void main(String[] args){ 5.Shape[] shape = new Shape[2]; 6.shape[0] = new Rectangle(5, 10); 7.shape[0] = new Square(10); 8. 9.for(int i=0; i< shape.length; i++) 10.System.out.println(shape[i]); 11.} 12.} As noted earlier, ics201 and ics201.graphics are independent packages, so the statement, import ics201.*; only imports ics201 package but not ics201.graphics.

Unit 0512 Importing an Entire Package- Cont’d Also note that the asterisk cannot be used to match a subset of a package. For example, the following does not match all the classes in the graphics package that begin with a: For convenience, the Java runtime system automatically imports three entire packages: oThe default package (the package with no name) oThe java.lang package o The current package What if two packages have classes with the same name and both packages are imported? -- You must use fully qualified names.

Unit 0513 Managing Source and Class Files Declaring package names at the beginning of source files is what is required to create a package. However, for the Java system to locate your source and class files, you must store them in a directory whose name matches the name of their package. For example, the source code for the Rectangle class should be in a file, Rectangle.java, and must be in a directory, ics201\graphics. The ics201\graphics directory can be anywhere on the file system.

Unit 0514 Managing Source and Class Files- Cont’d Note however, that the.class and.java files do not have to be in the same directory as shown below: Since the directory structure can be anywhere in the file system, it is obvious that the package name alone does not provide a java tool, such as the Compiler, with all the information need to locate a class. The remaining information is provided by CLASSPATH.

Unit 0515 Managing Source and Class Files- Cont’d A class path is an ordered list of directories or jar files in which to search for class files. Each directory listed in the class path is a top-level directory in which package directories appear. For example, suppose that both.java and.class files for the ics201.graphics package are stored in the same directory. Suppose also that the directory structure for the package is in a directory z:\cs, as shown below, then we must add z:\cs to the class path:

Unit 0516 Visibility Modifiers Now that we have learnt about packages, this is a good time to summarize what we learnt about visibility modifiers. Visibility modifiers are used to allow/deny access to other classes or interfaces. There are four of them, namely, public, private, and protected. The fourth, the default, does not have a keyword, but is assumed if none of the other three is specified.

Unit 0517 Example 1.package package1; 2.public class A{ 3. public A(){ 4. System.out.println("Inside Class A"); 5. } 6. int method1(int x){ 7. return x*x; 8. } 9. public int method2(int x){ 10. return method1(x) + method1(x); 11. } 12.}

Unit 0518 Example (cont’d) 1.package package2; 2.import package1.*; 3.public class B extends A{ 4. public B ( ){ System.out.println ("Inside Class B"); } 5. public int method1 ( int x ) { 6. A a = new B(); 7. return a.method2 (x); 8. } 9. public int method2 ( int x ){return x; } 10. public static void main ( String [] x ) { 11. B b = new B ( ); 12. System.out.println ( b.method1 ( 3 ) ); 13. } 14.}

Unit 0519 Exercises 1.Add the statement: package ics202.graphics; to each of our shape classes, except TestShape. Store all the files in some root folder. Try compiling and running the TestShape class. What happens? 2.Create a folder, ics201\graphics, and store the shape files in it, except the TestShape class. Do not put any import statement in the TestShape. Try compiling and running TestShape. What happens? 3.Add the import statement, import ics201.graphics.*; to TestShape Try compiling and running TestShape without giving the class path for the ics201.graphics package. What happens? 4.Try compiling and running TestShape, giving the class path for the ics201.graphics package. It should compile and run successfully. 5.Try each of the methods of importing classes discussed in this session and the TestShape class. 6.Add the import statement; java.awt.*; What happens? How can this problem be resolved?