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.

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

Java Packages CSci 1130 Intro to Computer Programming with Java Instructor Tatyana Volk.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Characteristics of OOP Encapsulation chap.5 Inheritance chap.6 Polymorphism chap.7.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
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-
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Creating a Java Application and Applet
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.
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.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
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
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
© 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.
© 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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
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 Development Packages
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
Sampath Kumar S Assistant Professor, SECE
Constructors, GUI’s(Using Swing) and ActionListner
PACKAGES.
Using java libraries CGS3416 spring 2019.
Chapter 6 Objects and Classes
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Interfaces,Packages and Threads
Presentation transcript:

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 limited to reusing the classes within a program. What if we need to use classes from other programs without physically copying them into the program under development ? In Java, this is achieved by using what is known as “packages”, a concept similar to “class libraries” in other languages.

Packages Packages are Java’s way of grouping a number of related classes together into a single unit. That means, packages act as “containers” for classes. The benefits of organising classes into packages are: –The classes contained in the packages of other programs/applications can be reused. –In packages classes can be unique compared with classes in other packages. That two classes in two different packages can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name. –Classes in packages can be hidden if we don’t want other packages to access them. –Packages also provide a way for separating “design” from coding.

Java Foundation Packages Java provides a large number of classes grouped into different packages based on their functionality. The six foundation Java packages are: –java.lang Contains classes for primitive types, strings, math functions, threads, and exception –java.io Stream classes for I/O –java.awt (abstract windowing kit) Classes for implementing GUI – windows, buttons, menus etc. –java.net Classes for networking –java.util Contains classes such as vectors, hash tables, date etc. –java.applet Classes for creating and implementing applets

Using System Packages The packages are organised in a hierarchical structure. For example, a package named “java” contains the package “awt”, which in turn contains various classes required for implementing GUI (graphical user interface).

Accessing Classes from Packages There are two ways of accessing the classes stored in packages: –Using fully qualified class name java.lang.Math.sqrt(x); –Import package and use class name directly. import java.lang.Math Math.sqrt(x); Selected or all classes in packages can be imported: Implicit in all programs: import java.lang.*; package statement(s) must appear first

Creating Packages Java supports a keyword called “package” for creating user- defined packages. The package statement must be the first statement in a Java source file (except comments and white spaces) followed by one or more classes. The file is saved as “ClassA.java” and code is located in directory “myPackage”. package myPackage; public class ClassA { // class body } class ClassB { // class body }

Creating Sub Packages Classes in one ore more source files can be part of the same packages. As packages in Java are organised hierarchically, sub-packages can be created as follows: –package myPackage.Math –package myPackage.secondPakage.thirdPackage Store “thirdPackage” in a subdirectory named “myPackage\secondPackage”. Store “secondPackage” and “Math” class in a subdirectory “myPackage”.

Accessing a Package The general form of importing package is: –import package1[.package2][…].classname –Example: import myPackage.ClassA; import myPackage.secondPackage.ClassC; –All classes/packages from higher-level package can be imported as follows: import myPackage.*;

Let us store the code listing below in a file named “ClassA.java” within subdirectory named “myPackage” within the current directory (say “abc”). package myPackage; public class ClassA { // class body public void display() { System.out.println("Hello, I am ClassA"); } class ClassB { // class body }

Using a Package Within the current directory (“abc”) store the following code in a file named “ClassX.java” import myPackage.ClassA; public class ClassX { public static void main(String args[]) { ClassA objA = new ClassA(); objA.display(); }

Compiling and Running When ClassX.java is compiled, the compiler compiles it and places.class file in current directly. If.class of ClassA in subdirectory “myPackage” is not found, it compiles ClassA also. Note: It does not include code of ClassA into ClassX When the program ClassX is run, java loader looks for ClassA.class file in a package called “myPackage” and loads it.

Using a Package Let us store the code listing below in a file named “ClassC.java” within subdirectory named “secondPackage” within the current directory (say “abc”). package secondPackage; public class ClassC { // class body public void display() { System.out.println("Hello, I am ClassC"); }

Within the current directory (“abc”) store the following code in a file named “ClassY.java” import myPackage.ClassA; import secondPackage.ClassC; public class ClassY { public static void main(String args[]) { ClassA objA = new ClassA(); ClassC objC = new ClassC(); objA.display(); objC.display(); }

Output Hello, I am ClassA Hello, I am ClassC

Sub Packages Packages can be divided into subpackages java.awt Classes for GUIs and graphics java.awt.font Classes and interface for fonts java.awt.geom Classes for 2-dimensional objects

Some Points Use fully qualified name: java.util.Scanner sc = new java.util.Scanner(System.in); Can use import to tell Java where to look for things defined outside your program import java.util.Scanner; Scanner sc = new Scanner (System.in); Using * imports all classes in package: import java.util.*; Scanner sc = new Scanner (System.in); Multiple import statements allowed java.lang automatically imported by every Java program

User Defined Packages Why? Packages enable a programmer organize the code into smaller logically related units A large program may consists of hundreds of classes (800 in one current project with NASA) Every class is part of some package If you do not specify a package a class becomes part of the default package Access Classes defined within the same package can access one another more easily (no need for imports, fully qualified names) Some classes, object fields only accessible to classes in same package

Accessing Packages To define: add package statement to specify package containing the classes of a file package mypackage; public class myClass { … } // myClass is part of mypackage Must be first non-comment statement in file Packages organized into subpackages using the notation package mypackage.mysubpackage;

Class Access and Packages Class access within a package Classes within a package can refer to each other without full qualification If a class, or member within a class, is not declared public, it can only be accessed by other classes within the package Class access across packages A public class can be accessed from other packages Its name is fully qualified or it must be imported to achieve this The public classes of a package can be seen as the interface of the package with the outside world Importing a package does not automatically import subpackages E.g. import java.awt.* does not import java.awt.font

Example

Example: graphics.shapes

Example: graphics.otherstuff

Example: graphics Package