SlideShare a Scribd company logo
Page 1 of 5
Class Notes on Packages (Week - 7)
Contents:-Creationof packages,importingpackages,memberaccessforpackages.
Java package
A Java package isa mechanismfororganizing Javaclassesintonamespaces .Javapackagescan be stored in compressed
filescalled JARfiles, allowingclassestodownloadfasterasagroup ratherthan one at a time.Programmersalsotypically
use packages to organize classes belonging to the same category or providing similar functionality.
 A package provides a unique namespace for the types it contains.
 Classes in the same package can access each other's package-access members.
In general, a package can contain the following kinds of types. A package allows a developer to group cl asses (and
interfaces) together.Theseclasseswill all be relatedinsome way – they might all have to do with a specific application
or perform a specific set of tasks. The Java API is a collection of packages – for example, the javax.xml package. The
javax.xml package and its subpackages contain classes to handle XML.
Packagesare usedinJavain-ordertopreventnamingconflicts,tocontrol access, to make searching/locating and usage
of classes, interfaces, enumerations and annotations easier etc.
A Package can be definedasa groupingof related types(classes, interfaces, enumerations and annotations ) providing
access protection and name space management.
Some of the existing packages in Java are:
 java.lang - bundles the fundamental classes
 java.io - classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes/interfaces etc. It is a good practice to group
related classes implemented by you so that a programmers can easily determine that the classes, interfaces,
enumerations, annotations are related.
Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using
packages, it is easier to provide access control and it is also easier to locate the related classed.
Creating a package:
When creating a package, you should choose a name for the package and put a package statement with that name at
the top of everysource file that contains the classes, interfaces, enumerations, and annotation types that you want to
include in the package.
The package statement should be the first line in the source file. There can be only one package statement in each
source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an
unnamed package.
Page 2 of 5
Example:
Let us look at an example that creates a package called animals. It is common practice to use lowercased names
of packages to avoid any conflicts with the names of classes, interfaces.
Put an interface in the package animals:
/* File name : Animal.java */
package animals;
interface Animal {
public void eat();
public void travel();
}
Now put an implementation in the same package animals:
package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Nowyoucompile these twofilesandputthemina sub-directorycalled animalsandtryto run as follows:
$ mkdir animals
$ cp Animal.class MammalInt.class animals
$ java animals/MammalInt
Mammal eats
Mammal travels
The import Keyword:
If a classwantsto use anotherclass inthe same package,the package name doesnotneedto be used.Classesinthe
same package findeachother withoutanyspecial syntax.
Page 3 of 5
Example:
Here a classnamedBossis addedtothe payroll package thatalreadycontainsEmployee. The Boss can then refer to the
Employee class without using the payroll prefix, as demonstrated by the following Boss class.
package payroll;
public class Boss
{
public void payEmployee(Employee e)
{
e.mailCheck();
}
}
What happens if Boss is not in the payroll package? The Boss class must then use one of the following techniques for
referring to a class in a different package.
 The fully qualified name of the class can be used. For example:
payroll.Employee
 The package can be imported using the import keyword and the wild card (*). For example:
import payroll.*;
 The class itself can be imported using the import keyword. For example:
import payroll.Employee;
Note:A class file cancontainanynumber of import statements. The import statements must appear after the package
statement and before the class declaration.
The Directory Structure ofPackages:
Two major results occur when a class is placed in a package:
 The name of the package becomesapart of the name of the class, as we just discussed in the previous section.
 The name of the package must match the directory structure where the corresponding bytecode resides.
Here is simple way of managing your files in java:
Put the source code for a class,interface,enumeration,orannotationtype inatextfile whose name is the simple name
of the type and whose extension is .java. For example:
// File Name : Car.java
package vehicle;
public class Car {
// Class implementation.
}
Nowput the source file ina directorywhose name reflectsthe name of the package towhichthe class belongs:
Page 4 of 5
....vehicleCar.java
Nowthe qualifiedclassname andpathname wouldbe asbelow:
 Classname -> vehicle.Car
 Path name -> vehicleCar.java(inwindows)
In general a company uses its reversed Internet domain name for its package names. Example: A company's Internet
domain name is apple.com, then all its package names would start with com.apple. Each component of the package
name corresponds to a subdirectory.
Example:The companyhada com.apple.computerspackage thatcontainedaDell.javasource file,itwouldbe contained
in a series of subdirectories like this:
....comapplecomputersDell.java
At the time of compilation,the compilercreatesadifferentoutputfile foreachclass,interface andenumerationdefined
in it. The base name of the output file is the name of the type, and its extension is .class
For example:
// File Name: Dell.java
package com.apple.computers;
public class Dell{
}
class Ups{
}
Nowcompile thisfile asfollowsusing -doption:
$javac -d . Dell.java
This would put compiled files as follows:
.comapplecomputersDell.class
.comapplecomputersUps.class
You can import all the classes or interfaces defined in comapplecomputers as follows:
import com.apple.computers.*;
Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name.
However,the pathto the .classfilesdoesnot have to be the same as the path to the .java source files. You can arrange
your source and class directories separately, as:
<path-one>sourcescomapplecomputersDell.java
<path-two>classescomapplecomputersDell.class
By doingthis,itispossible togive the classesdirectorytootherprogrammerswithoutrevealingyoursources.You also
needtomanage source and class filesinthismannersothatthe compilerandthe Java Virtual Machine (JVM) canfindall
the typesyourprogram uses.
Page 5 of 5
The full pathto the classesdirectory,<path-two>classes,iscalledthe classpath,and is set with the CLASSPATH system
variable.Boththe compilerandthe JVMconstruct the path to your .class files by adding the package name to the class
path.
Say <path-two>classesisthe classpath,and the package name is com.apple.computers,thenthe compiler andJVMwill
look for .class files in <path-two>classescomapplecomptuers.
A classpath may include several paths.Multiple pathsshouldbe separatedbyasemicolon(Windows) orcolon(Unix).By
default,the compilerand the JVMsearch the current directory and the JAR file containing the Java platform classes so
that these directories are automatically in the class path.
Set CLASSPATH System Variable:
To display the current CLASSPATH variable, use the following commands in Windows and Unix (Bourne shell ):
 In Windows -> C:> set CLASSPATH
 In Unix -> % echo $CLASSPATH
To delete the current contents of the CLASSPATH variable, use :
 In Windows -> C:> set CLASSPATH=
 In Unix -> % unset CLASSPATH; export CLASSPATH
To set the CLASSPATH variable:
 In Windows -> set CLASSPATH=C:usersjackjavaclasses
 In Unix -> % CLASSPATH=/home/jack/java/classes; export CLASSPATH
Package naming conventions
Packagesare usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods ( .,
pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the
correspondingpackageshigher in the hierarchy, there is almost no semantic relationship between packages. The Java
Language Specification establishes package naming conventions to avoid the possibility of two published packages
havingthe same name. The naming conventions describe how to create unique package names, so that packages that
are widely distributed will have unique namespaces. This allows packages to be separately, easily and automatically
installed and catalogued.
In general, a package name begins with the top level domain name of the organization and then the organization's
domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its
package. Package names should be all lowercase characters whenever possible.
For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package
ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a
Germancompany namedMySoftalsocreatesa fractionspackage, but names it de.mysoft.fractions, then the classes in
these two packages are defined in a unique and separate namespace.
Complete conventions for disambiguating package names and rules for naming packages when the Internet domain
name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.
Good precise tutorial on java packages: http://www.jarticles.com/package/package_eng.html

More Related Content

What's hot

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
Fernando Gil
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
Kalai Selvi
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
Deepak Sharma
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
vanithaRamasamy
 
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Java packages
Java packagesJava packages
Java packages
BHUVIJAYAVELU
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
DevaKumari Vijay
 
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
Packages in java
Packages in javaPackages in java
Packages in java
Jerlin Sundari
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Srinivas Reddy
 
Packages in java
Packages in javaPackages in java
Packages in java
Jancypriya M
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Java package
Java packageJava package
Java package
Arati Gadgil
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 

What's hot (20)

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Java packages
Java packagesJava packages
Java packages
 
Java packages
Java packagesJava packages
Java packages
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
Java program structure
Java program structureJava program structure
Java program structure
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Packages in java
Packages in javaPackages in java
Packages in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Packages
PackagesPackages
Packages
 
Java package
Java packageJava package
Java package
 
Java packages
Java packagesJava packages
Java packages
 

Similar to Class notes(week 7) on packages

Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
KarthigaGunasekaran1
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
arnold 7490
 
Packages
PackagesPackages
Packages
Monika Mishra
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
Mavoori Soshmitha
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
Narayana Swamy
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
R.K.College of engg & Tech
 
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
Daniel Ilunga
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
mrecedu
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
Prashant Mokani
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
VeenaNaik23
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
talha ijaz
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Java Packages
Java Packages Java Packages

Similar to Class notes(week 7) on packages (20)

Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Packages
PackagesPackages
Packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Practice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docxPractice Program-9-Packages-Unit 4.docx
Practice Program-9-Packages-Unit 4.docx
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Java Packages
Java Packages Java Packages
Java Packages
 

More from Kuntal Bhowmick

Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loopsMultiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Kuntal Bhowmick
 
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Kuntal Bhowmick
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
1. introduction to E-commerce
1. introduction to E-commerce1. introduction to E-commerce
1. introduction to E-commerce
Kuntal Bhowmick
 
Computer graphics question for exam solved
Computer graphics question for exam solvedComputer graphics question for exam solved
Computer graphics question for exam solved
Kuntal Bhowmick
 
DBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsDBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental concepts
Kuntal Bhowmick
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
Kuntal Bhowmick
 
Operating system Interview Questions
Operating system Interview QuestionsOperating system Interview Questions
Operating system Interview Questions
Kuntal Bhowmick
 
Computer Network Interview Questions
Computer Network Interview QuestionsComputer Network Interview Questions
Computer Network Interview Questions
Kuntal Bhowmick
 
C interview questions
C interview  questionsC interview  questions
C interview questions
Kuntal Bhowmick
 
C question
C questionC question
C question
Kuntal Bhowmick
 
Distributed operating systems cs704 a class test
Distributed operating systems cs704 a class testDistributed operating systems cs704 a class test
Distributed operating systems cs704 a class test
Kuntal Bhowmick
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
Kuntal Bhowmick
 

More from Kuntal Bhowmick (20)

Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 8 -- int...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
Multiple Choice Questions on JAVA (object oriented programming) bank 7 -- abs...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
Multiple Choice Questions on JAVA (object oriented programming) bank 6 -- inh...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
Multiple Choice Questions on JAVA (object oriented programming) bank 5 -- mem...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loopsMultiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
 
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
Multiple Choice Questions on JAVA (object oriented programming) bank 3 -- cla...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
 
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
Multiple Choice Questions on JAVA (object oriented programming) bank 1 -- int...
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
1. introduction to E-commerce
1. introduction to E-commerce1. introduction to E-commerce
1. introduction to E-commerce
 
Computer graphics question for exam solved
Computer graphics question for exam solvedComputer graphics question for exam solved
Computer graphics question for exam solved
 
DBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsDBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental concepts
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Operating system Interview Questions
Operating system Interview QuestionsOperating system Interview Questions
Operating system Interview Questions
 
Computer Network Interview Questions
Computer Network Interview QuestionsComputer Network Interview Questions
Computer Network Interview Questions
 
C interview questions
C interview  questionsC interview  questions
C interview questions
 
C question
C questionC question
C question
 
Distributed operating systems cs704 a class test
Distributed operating systems cs704 a class testDistributed operating systems cs704 a class test
Distributed operating systems cs704 a class test
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 

Recently uploaded

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
architagupta876
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
MiscAnnoy1
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

Class notes(week 7) on packages

  • 1. Page 1 of 5 Class Notes on Packages (Week - 7) Contents:-Creationof packages,importingpackages,memberaccessforpackages. Java package A Java package isa mechanismfororganizing Javaclassesintonamespaces .Javapackagescan be stored in compressed filescalled JARfiles, allowingclassestodownloadfasterasagroup ratherthan one at a time.Programmersalsotypically use packages to organize classes belonging to the same category or providing similar functionality.  A package provides a unique namespace for the types it contains.  Classes in the same package can access each other's package-access members. In general, a package can contain the following kinds of types. A package allows a developer to group cl asses (and interfaces) together.Theseclasseswill all be relatedinsome way – they might all have to do with a specific application or perform a specific set of tasks. The Java API is a collection of packages – for example, the javax.xml package. The javax.xml package and its subpackages contain classes to handle XML. Packagesare usedinJavain-ordertopreventnamingconflicts,tocontrol access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc. A Package can be definedasa groupingof related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management. Some of the existing packages in Java are:  java.lang - bundles the fundamental classes  java.io - classes for input , output functions are bundled in this package Programmers can define their own packages to bundle group of classes/interfaces etc. It is a good practice to group related classes implemented by you so that a programmers can easily determine that the classes, interfaces, enumerations, annotations are related. Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classed. Creating a package: When creating a package, you should choose a name for the package and put a package statement with that name at the top of everysource file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.
  • 2. Page 2 of 5 Example: Let us look at an example that creates a package called animals. It is common practice to use lowercased names of packages to avoid any conflicts with the names of classes, interfaces. Put an interface in the package animals: /* File name : Animal.java */ package animals; interface Animal { public void eat(); public void travel(); } Now put an implementation in the same package animals: package animals; /* File name : MammalInt.java */ public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int noOfLegs(){ return 0; } public static void main(String args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } } Nowyoucompile these twofilesandputthemina sub-directorycalled animalsandtryto run as follows: $ mkdir animals $ cp Animal.class MammalInt.class animals $ java animals/MammalInt Mammal eats Mammal travels The import Keyword: If a classwantsto use anotherclass inthe same package,the package name doesnotneedto be used.Classesinthe same package findeachother withoutanyspecial syntax.
  • 3. Page 3 of 5 Example: Here a classnamedBossis addedtothe payroll package thatalreadycontainsEmployee. The Boss can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss class. package payroll; public class Boss { public void payEmployee(Employee e) { e.mailCheck(); } } What happens if Boss is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package.  The fully qualified name of the class can be used. For example: payroll.Employee  The package can be imported using the import keyword and the wild card (*). For example: import payroll.*;  The class itself can be imported using the import keyword. For example: import payroll.Employee; Note:A class file cancontainanynumber of import statements. The import statements must appear after the package statement and before the class declaration. The Directory Structure ofPackages: Two major results occur when a class is placed in a package:  The name of the package becomesapart of the name of the class, as we just discussed in the previous section.  The name of the package must match the directory structure where the corresponding bytecode resides. Here is simple way of managing your files in java: Put the source code for a class,interface,enumeration,orannotationtype inatextfile whose name is the simple name of the type and whose extension is .java. For example: // File Name : Car.java package vehicle; public class Car { // Class implementation. } Nowput the source file ina directorywhose name reflectsthe name of the package towhichthe class belongs:
  • 4. Page 4 of 5 ....vehicleCar.java Nowthe qualifiedclassname andpathname wouldbe asbelow:  Classname -> vehicle.Car  Path name -> vehicleCar.java(inwindows) In general a company uses its reversed Internet domain name for its package names. Example: A company's Internet domain name is apple.com, then all its package names would start with com.apple. Each component of the package name corresponds to a subdirectory. Example:The companyhada com.apple.computerspackage thatcontainedaDell.javasource file,itwouldbe contained in a series of subdirectories like this: ....comapplecomputersDell.java At the time of compilation,the compilercreatesadifferentoutputfile foreachclass,interface andenumerationdefined in it. The base name of the output file is the name of the type, and its extension is .class For example: // File Name: Dell.java package com.apple.computers; public class Dell{ } class Ups{ } Nowcompile thisfile asfollowsusing -doption: $javac -d . Dell.java This would put compiled files as follows: .comapplecomputersDell.class .comapplecomputersUps.class You can import all the classes or interfaces defined in comapplecomputers as follows: import com.apple.computers.*; Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However,the pathto the .classfilesdoesnot have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as: <path-one>sourcescomapplecomputersDell.java <path-two>classescomapplecomputersDell.class By doingthis,itispossible togive the classesdirectorytootherprogrammerswithoutrevealingyoursources.You also needtomanage source and class filesinthismannersothatthe compilerandthe Java Virtual Machine (JVM) canfindall the typesyourprogram uses.
  • 5. Page 5 of 5 The full pathto the classesdirectory,<path-two>classes,iscalledthe classpath,and is set with the CLASSPATH system variable.Boththe compilerandthe JVMconstruct the path to your .class files by adding the package name to the class path. Say <path-two>classesisthe classpath,and the package name is com.apple.computers,thenthe compiler andJVMwill look for .class files in <path-two>classescomapplecomptuers. A classpath may include several paths.Multiple pathsshouldbe separatedbyasemicolon(Windows) orcolon(Unix).By default,the compilerand the JVMsearch the current directory and the JAR file containing the Java platform classes so that these directories are automatically in the class path. Set CLASSPATH System Variable: To display the current CLASSPATH variable, use the following commands in Windows and Unix (Bourne shell ):  In Windows -> C:> set CLASSPATH  In Unix -> % echo $CLASSPATH To delete the current contents of the CLASSPATH variable, use :  In Windows -> C:> set CLASSPATH=  In Unix -> % unset CLASSPATH; export CLASSPATH To set the CLASSPATH variable:  In Windows -> set CLASSPATH=C:usersjackjavaclasses  In Unix -> % CLASSPATH=/home/jack/java/classes; export CLASSPATH Package naming conventions Packagesare usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods ( ., pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the correspondingpackageshigher in the hierarchy, there is almost no semantic relationship between packages. The Java Language Specification establishes package naming conventions to avoid the possibility of two published packages havingthe same name. The naming conventions describe how to create unique package names, so that packages that are widely distributed will have unique namespaces. This allows packages to be separately, easily and automatically installed and catalogued. In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Package names should be all lowercase characters whenever possible. For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a Germancompany namedMySoftalsocreatesa fractionspackage, but names it de.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace. Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification. Good precise tutorial on java packages: http://www.jarticles.com/package/package_eng.html