Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.

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

Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods.
Unit2: Object-oriented programming Getting started with Java Jin Sa.
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 CSE 331 Java Packages; JAR Archives slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
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.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
1 Lecture 2: Object Oriented Programming in Java.
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.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 4 Methods 条条大路通罗马.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
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.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Packages. Creating and Using Packages To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Package A package is a logical container that contains classes,interfaces sub packages. Package provide a unique name space to its members and provide.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
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.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Packages In Java.
Chapter 7 User-Defined Methods.
Formatting Output & Enumerated Types & Wrapper Classes
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Packages When we name a program , we name it by class name…
Java Basics Packages.
Chapter 5 Methods.
Packages Written part of final exam Alex Rudniy
Package & Java Access Specifiers
Chapter 5 Methods.
Introduction to Computer Science and Object-Oriented Programming
Packages From Deitel & Deitel.
Object Oriented Programming in java
Applying OO Concepts Using Java
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

Packages

Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently. Packages group related classes so that they can be easily distributed. 3.Protect classes. Protected members of the classes are accessible to the classes in the same package, but not to other classes.

Package-Naming Conventions java.lang.Math java.lang.Math indicates that Math is a class in the package java.lang. package edu.kmartin.homepage.myproject.myjava Use Internet domain name in reverse order as a package prefix to prevent naming conflicts. public class MyClass { } MyClass.class MUST be stored in edu/kmartin/homepage/myproject/myjava/ Or (. Means look in current directory first) package myproject.myjava; Set classpath=.;edu.kmartin.homepage;

Package Directories For the package named com.prenhall.mypackage, create directories. A package is actually a set of directories that contains the bytecode of the classes. com.prenhall.mypackage The com directory does not have to be the root directory. In order for Java to know where your package is in the file system, you must modify the environment variable classpath so that it points to the directory in which your package resides.

Setting classpath Environment Suppose the com directory is under c:\book. The following line adds c:\book into the classpath: classpath=.;c:\book; package com.prenhall.mypackage

Putting Classes into Packages Every class in Java is in a package (current directory is the default unnamed package.) The class is added to the package when it is compiled. To avoid the default, add the following line as the first non comment and nonblank statement in the program: package packagename;

Putting Classes into Packages Problem This example creates a class named Format and places it in the package com.prenhall.mypackage. The Format class contains the format method that returns a new number with the specified number of digits after the decimal point. For example, format( , 2) returns 10.34, and format( , 3) returns – Solution 1. Create Format.java as follows and save it into c:\book\com\prenhall\mypackage. // Format.java: Format number. package com.prenhall.mypackage; public class Format { public static double format( double number, int numOfDecimalDigits) { return Math.round(number * Math.pow(10, numOfDecimalDigits)) / Math.pow(10, numOfDecimalDigits); } 2. Compile Format.java. Make sure Format.class is in c:\book\com\prenhall\mypackage.

Using Classes from Packages Two ways to use classes from a package. Use the fully qualified name of the class. The fully qualified name for JOptionPane is javax.swing.JOptionPane. Use the import statement. To import all the classes in the javax.swing package import javax.swing.*; import javax.swing.JOptionPane; The information for the classes in an imported package is not read in at compile time or runtime unless the class is used in the program.

Using Packages Using the Format class in the com.prenhall.mypackage.mypackage package. Solution 1. Create TestFormatClass.java as follows and save it into c:\book. // TestFormatClass.java: Demonstrate using the Format class import com.prenhall.mypackage.Format; public class TestFormatClass { /** Main method */ public static void main(String[] args) { System.out.println(Format.format( , 2)); System.out.println(Format.format( , 3)); }