DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.

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

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Getting Started with Java
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
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.
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.
1 Lecture 2: Object Oriented Programming in Java.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
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.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
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.
Introduction to programming in the Java programming language.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
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.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
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.
Building Packages BCIS 3680 Enterprise Programming.
Introduction to OOP CPS235: Introduction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
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.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Working with Packages BCIS 3680 Enterprise Programming.
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.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Packages In Java.
“Packages in Java”.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Packages When we name a program , we name it by class name…
Java Basics Packages.
CSC240 Computer Science III
Packages Written part of final exam Alex Rudniy
Packages and Interfaces
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
Applying OO Concepts Using Java
PACKAGES.
Object Oriented Programming in java
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages

Definition: A package is a grouping of related classes and interfaces providing access protection and name space management. One of the main features of Object Oriented Programming is its ability to reuse the code that is already created. If it is necessary to use the classes from other programs without physically copying them into a new program, packages can be used. In Java packages are the way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to the functionality of the class. In fact, packages act as “containers” for classes. 2 Introduction

By organizing the classes into packages the following benefits can be achieved. 1.The classes contained in the packages of other programs can be easily reused. 2. In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name. They may be referred by their fully qualified name (package name and class name). 3. Packages provide a way to hide classes thus preventing other programs or packages from accessing classes that are meant for internal use only 3

Classification of packages In Java packages are classified into two types, 1. Java API packages. 2. user defined packages. Java API packages. Java API (Application Programming Interface) provides a large number of classes grouped into different packages according to functionality. Following figure shows some of the frequently used packages 4

API packages Using System Packages. The classes in the Java API are organized in to a hierarchical structure. Two ways to access the classes stored in a package. The first approach is to use the fully qualified class name of the class that need to be used. This is done by using the package name containing the class and then appending the class name to it using the dot operator. example Vector in the util package, java.util.Vector v = new java.util.Vector(); note that the util is a package within the package java and the hierarchy is represented by separating the levels with dots. 5

API packages… In many situations it is necessary to use a class in a number of places in the program or may need to use many of the classes contained in the same package. This can be easily done by using the import statement. import packagename.classname; or import packagename.*; (* represents all the classes of the package) These are known as import statements and must appear at the top of the file, before any class declaration. 6

API packages… Example import java.awt.Color; imports the class Color and therefore the class name can now be directly used in the program. There is no need to use the package name to qualify the class. The following statement imports all the classes contained in the java.awt package. import java.awt.*; 7

API packages… Naming conventions Packages can be named using the standard Java naming rules. By convention, however, packages begin with lowercase letters. This makes it easy for users to distinguish package names from class names when looking at an explicit reference to a class. 8

User Defined Packages Creating the package. Java provides the facility to create user defined packages. Need to perform the following steps. 1. Declare the package at the beginning of a file using the statement, package packagename; 2. Define the class that is to be put in the package and declare it public. 3. Create a subdirectory under the directory where the main source files are stored. 4. Store the classname.java files in the subdirectory created. Note that the subdirectory name must match the package name exactly. 9

User Defined Packages… Accessing a package Similar way as accessing the API packages, the import command is used; import package1 [.package2] [.package3].classname; Or import packagename.* ; 10

User Defined Packages… Example: This is a Java example of using a package for a application to handle employee details of a company. Step 1: Code the Java file giving a package name. package company; public class Employee {private String name; public void setName(String name) { this.name=name; } public String getName() { return this.name; } 11

User Defined Packages… Step 2: Save the file with the name “Employee.java” in a folder “company” (Eg: D:\ Java \ company) Step 3: import the Employee class in the company package into the driver class. Save the driver class in the directory D: \ Java. import company.Employee; class Test { public static void main(String args[]) { Employee e1=new Employee(); e1.setName("Nimal Perera"); System.out.println(e1.getName()); } 12

User Defined Packages… Note that in this example the driver class is saved in the directory D: \ Java. Therefore it is possible to give the relative path into the Employee class in the import statement as company.Employee (or company.*). But if the driver class is stored in a directory which can not resolve the relative path to the company package it will produce a compile time error. 13

User Defined Packages… To solve this problem, need to set the absolute path to the place where the package is stored. Then from that place the relative path company.Employee will be considered. This is done by setting the classpath environmental variable. In the command prompt type; set classpath =.;D:\Java Note that the symbol “.” before the absolute path (D:\Java) is to represent the current working directory of Java. When this part is omitted the interpreter may not be able to find the class file of the driver class 14