SlideShare a Scribd company logo
WELCOME TOWELCOME TO
OUROUR
PRESENTATIONPRESENTATION
Packages
• Package is a container for classes
• A package is a grouping of related types
(classes and interfaces) providing access
protection and name space management.
• In simple words, packages is the way we
organize files into different directories
according to their functionality, usability as
well as category they should belong to.
Why do we need Packages?
• One can easily determine that these types are related.
• One knows where to find types that can provide task-
related functions.
• The names of your types won't conflict with the type
names in other packages because the package creates a
new namespace.
• One can allow types within the package to have
unrestricted access to one another yet still restrict access
for types outside the package.
How To Create a Package
• Packages are mirrored through directory
structure.
• To create a package, First we have to create
a directory /directory structure that matches
the package hierarchy.
• Package structure should match the
directory structure also.
• To make a class belongs to a particular
package include the package statement as
the first statement of source file.
IntroductionIntroduction
Javapackages 4th semester
Javapackages 4th semester
Javapackages 4th semester
Javapackages 4th semester
Package access protection
• Classes within a package can access
classes and members declared with
default access and class members
declared with the protected access
modifier.
• Default access is enforced when neither
the public, protected nor private access
modifier is specified in the
declaration.
Exercise Creating Packages
my package
mypackageBmypackageA
ABC DEG IJK XYZ
S1,S2,S
3
S4,S5,S
6
A,B,CA,B,C D,E,FD,E,F
A,B,C,I,J,KA,B,C,I,J,K X,Y,ZX,Y,Z
Package ABC and IJK have classes with same name.Package ABC and IJK have classes with same name.
A class in ABC has nameA class in ABC has name mypackage.mypackageA.ABC.A
A class in IJK has nameA class in IJK has name mypackage.mypackageB.IJK.A
How to make a class Belong to a
Package
• Include a proper package statement as first line in source file
Make class S1 belongs to mypackageA
package mypackage.mypackageA;
public class S1
{
public S1( )
{
System.out.println("This is Class
S1");
}
} Name the source file as S1.java
and compile it and store the
S1.class file in mypackageA
directory
Make class S2 belongs to mypackageA
package mypackage.mypackageA;
public class S2
{
public S2( )
{
System.out.println("This is Class
S2");
}
}
Name the source file as S2.javaName the source file as S2.java
and compile it and store theand compile it and store the
S2.class file in mypackageAS2.class file in mypackageA
directorydirectory
Make class A belongs to IJK
package
mypackage.mypackageB.IJK;
public class A
{
public A( )
{
System.out.println("This is
Class A in IJK");
}
}
Name the source file as A.java
and compile it and store the
A.class file in IJK directory
<< Same Procedure For all
classes>>
Importing the PackageImporting the Package
• import statement allows the importing of package
• Library packages are automatically imported
irrespective of the location of compiling and executing
program
• JRE looks at two places for user created packages
(i) Under the current working directory
(ii) At the location specified by CLASSPATH
environment variable
• Most ideal location for compiling/executing a program
is immediately above the package structure.
Example importingExample importing
import mypackage.mypackageA.ABC;import mypackage.mypackageA.ABC;
import mypackage.mypackageA.ABC.*;import mypackage.mypackageA.ABC.*;
class packagetestclass packagetest
{{
public static void main(String args[])public static void main(String args[])
{{
B b1 = new B();B b1 = new B();
C c1 = new C();C c1 = new C();
}}
}}
<<packagetest.java>>
<< Store it in location above the<< Store it in location above the
package structure. Compile andpackage structure. Compile and
Execute it from there>>Execute it from there>>
<< Store it in location above the<< Store it in location above the
package structure. Compile andpackage structure. Compile and
Execute it from there>>Execute it from there>>
import mypackage.mypackageA.ABC.*;
Import mypackage.mypackageB.IJK.*;
class packagetest
{
public static void main(String args[])
{
A a1 = new A();
}
}
<< What’s Wrong
Here>>
<< class A is present in both the
imported packages ABC and IJK.
So A has to be fully qualified in
this case>>
mypackage.mypackageA.ABC.A a1 = new
mypackage.mypackageA.ABC.A();
OR
mypackage.mypackageB.IJK.A a1 = new
mypackage.mypackageB.IJK.A();
Using Packages
– Class in a named package can be referred
to in two different ways
• Using the fully qualified name
packagename.ClassName
• We can refer to the ElevatorPanel class in
package elevator as
elevator.ElevatorPlanel
Common Mistakes
• Common mistakes while running the program
– The directory structure for elevator program is
C:Projectelevator.
– Run the program from elevator directory, and we will get the
following error message
c:projectelevator>java ElevatorSimulation
Exception in thread "main"
java.lang.NoClassDefFoundError: ElevatorSimulation
– The program runs successfully by running the program from
c:project directory.
Solution
• Add c:project to the CLASSPATH,
and rerun the program.
• The program is launched
successfully without any error
messages.
Nested Classes
Nested classes are defined WITHIN another class
They are defined using the static keyword
The nested class is defined as part of the outer class. It can
only be referenced through the outer class.
public class LinkedList
{
private static class LinkedListNode
{
[...]
}
public static class LinkedListStats
{[...]
}// Create an instance of the nested class LinkedListStats
LinkedList.LinkedListStats x = new LinkedList.LinkedListStats();
// Illegal access to private nested class -- compiler error
dLinkeList.LinkedListNode y = new LinkedList.LinkedListNode();
Local Classes
Local classes are defined within a method
The scope is limited to the method
Used for small helper classes whose applicability is within the method only
public class LinkedList
{
public void traverse()
{
class MyIterator extends Iterator
{
public void doTraversal()
{
[...]
}
}
MyIterator anIterator = new MyIterator();
[...]
}
}
Member Classes
Member classes are similar to nested classes, except they
are not static.
They are used in the same manner as instance variables.
public class LinkedListpublic class LinkedList
{{
public class LinkedListStatspublic class LinkedListStats
{{
[...][...]
}} LinkedList aList = new Linked List();LinkedList aList = new Linked List();
LinkedList.LinkedListStats aStat = newLinkedList.LinkedListStats aStat = new
aList.LinkedListStats();aList.LinkedListStats();
Access to members of the
classes
Java packages can be stored in compressed files called JAR files,
allowing classes to download faster as a group rather than one at a time.
Demo:Access modifier
privatepackage p1
class C1
private int x
class C2 extends C1
C1 c1;
c1.x cannot be read
or modified
package p2
class C4 extends C1
x cannot be read
or modified in C2
class C5
C1 c1;
c1.x cannot be read
nor modifiedClass C3
C1 c1;
c1.x cannot be read
or modified
Default access modifier
private protected
package p1
class C1
private int x
class C2 extends C1
C1 c1;
c1.x can be read or
modified
package p2
class C4 extends C1
C1 c1;
c1.x can be read or
modified
class C5
C1 c1;
c1.x cannot be read
nor modifiedClass C3
C1 c1;
c1.x cannot be read
or modified
Access modifier friendly
package p1
class C1
private int x
class C2 extends C1
C1 c1;
c1.x can be read or
modified
package p2
class C4 extends C1
C1 c1;
c1.x cannot be
read nor modified
class C5
C1 c1;
c1.x cannot be read
nor modifiedClass C3
C1 c1;
c1.x can be read or
modified
Access modifier friendly
package p1
class C1
private int x
class C2 extends C1
C1 c1;
c1.x can be read or
modified
package p2
class C4 extends C1
C1 c1;
c1.x can be read or
modified
class C5
C1 c1;
c1.x cannot be read
nor modifiedClass C3
C1 c1;
c1.x can be read or
modified
Access modifier public
package p1
class C1
public int x
class C3
C1 c1;
c1.x can be read or
modified
package p2
class C2 extends C1
x can be read or
modified in C2
class C4
C1 c1;
c1.x can be read nor
modified
 Suppose you write a group of classes that representSuppose you write a group of classes that represent
graphic objects, such as circles, rectangles, lines, andgraphic objects, such as circles, rectangles, lines, and
pointspoints
●● You also write an interface, Draggable, that classesYou also write an interface, Draggable, that classes
implement if they can be dragged with the mouseimplement if they can be dragged with the mouse
//in the Draggable.java file//in the Draggable.java file
public interface Draggable {}public interface Draggable {}
//in the Graphic.java file//in the Graphic.java file
public abstract class Graphic {}public abstract class Graphic {}
//in the Circle.java file//in the Circle.java file
public class Circle extends Graphic implements Draggable {}public class Circle extends Graphic implements Draggable {}
//in the Rectangle.java file//in the Rectangle.java file
public class Rectangle extends Graphic implements Draggable { }public class Rectangle extends Graphic implements Draggable { }
//in the Point.java file//in the Point.java file
public class Point extends Graphic implements Draggable {}public class Point extends Graphic implements Draggable {}
//in the Line.java file//in the Line.java file
public class Line extends Graphic implements Draggable {}public class Line extends Graphic implements Draggable {}
 Suppose you write a group of classes that representSuppose you write a group of classes that represent
graphic objects, such as circles, rectangles, lines, andgraphic objects, such as circles, rectangles, lines, and
pointspoints
●● You also write an interface, Draggable, that classesYou also write an interface, Draggable, that classes
implement if they can be dragged with the mouseimplement if they can be dragged with the mouse
//in the Draggable.java file//in the Draggable.java file
public interface Draggable {}public interface Draggable {}
//in the Graphic.java file//in the Graphic.java file
public abstract class Graphic {}public abstract class Graphic {}
//in the Circle.java file//in the Circle.java file
public class Circle extends Graphic implements Draggable {}public class Circle extends Graphic implements Draggable {}
//in the Rectangle.java file//in the Rectangle.java file
public class Rectangle extends Graphic implements Draggable { }public class Rectangle extends Graphic implements Draggable { }
//in the Point.java file//in the Point.java file
public class Point extends Graphic implements Draggable {}public class Point extends Graphic implements Draggable {}
//in the Line.java file//in the Line.java file
public class Line extends Graphic implements Draggable {}public class Line extends Graphic implements Draggable {}
Example PackageExample Package
Example: PlacingExample: Placing
StudentRecordStudentRecord
class in SchoolClassesclass in SchoolClasses
pacakgepacakge
package SchoolClasses;package SchoolClasses;
public class StudentRecord {public class StudentRecord {
private String name;private String name;
private String address;private String address;
private int age;private int age;
::
Packages – Directory Paths
• The CLASSPATH
– List of directories and/or jar files. The compiler will
look in these directories for any precompiled files it
needs.
– The CLASSPATH can be set as an environmental
variable or specified on the command line using –
classpath option.
– The CLASSPATH is also used by the Java Virtual
Machine to load classes.
Compile Package Classes
• Compile the program
– To compile the program, we must change the working
directory to the source directory root, and issue the following
command
c:project> javac -d . elevator*.java
– By compiling everything, we get the recent version of all the
classes.
– Specify –d . option to tell the compiler to put the classes in a
package structure starting at the root.
Benefits of Packaging
●
You and other programmers can easily determine thatYou and other programmers can easily determine that
these classes and interfaces are related.these classes and interfaces are related.
●
You and other programmers know where to find classesYou and other programmers know where to find classes
and interfaces that can provide graphicsrelated functions.and interfaces that can provide graphicsrelated functions.
●
The names of your classes and interfaces won't conflictThe names of your classes and interfaces won't conflict
with the names in other packages because the packagewith the names in other packages because the package
creates a new namespace.creates a new namespace.
●
You can allow classes within the package to haveYou can allow classes within the package to have
unrestricted access to one another yet still restrict accessunrestricted access to one another yet still restrict access
for types outside the package.for types outside the package.
THANKS TO ALLTHANKS TO ALL
ID:141311056ID:141311056
ID:141311057ID:141311057
ID:141311058ID:141311058
ID:141311059ID:141311059
ID:141311060ID:141311060
ID:141311062ID:141311062
ID:141311063ID:141311063
special thanks to-special thanks to-
NOYONNOYON

More Related Content

PPTX
Package in Java
PPTX
Java packages
PPT
Java packages
PPT
Packages in java
PPTX
OCA JAVA - 1 Packages and Class Structure
PPT
Packages and interfaces
PDF
Java - Packages Concepts
PPT
Java package
Package in Java
Java packages
Java packages
Packages in java
OCA JAVA - 1 Packages and Class Structure
Packages and interfaces
Java - Packages Concepts
Java package

What's hot (20)

PPTX
Packages,static,this keyword in java
PPTX
Java package
PPTX
Introduction to package in java
PPTX
Java packages
PPTX
Package In Java
PPTX
Pi j4.1 packages
PPTX
Packages in java
PPTX
Packages in java
PPT
PPT
Packages in java
PPTX
5.interface and packages
PDF
Java - Interfaces & Packages
PDF
Access modifiers
PPT
9 cm604.26
PDF
javapackage
PPT
Packages,interfaces and exceptions
PPTX
Java program structure
PPT
Java access modifiers
DOCX
Class notes(week 7) on packages
PPT
Java non access modifiers
Packages,static,this keyword in java
Java package
Introduction to package in java
Java packages
Package In Java
Pi j4.1 packages
Packages in java
Packages in java
Packages in java
5.interface and packages
Java - Interfaces & Packages
Access modifiers
9 cm604.26
javapackage
Packages,interfaces and exceptions
Java program structure
Java access modifiers
Class notes(week 7) on packages
Java non access modifiers
Ad

Viewers also liked (9)

PPTX
Plataformas de internet y servicios en línea
PPTX
Generadores eolicos
PDF
Report Rai 3 a Expo: l’imbarazzante presenza di Coca-Cola, Ferrero e McDonald...
DOC
56 2011 tt-bgtvt
PPTX
(You better) change focus, 2015 finance ict & isaca v2
PPTX
2015 xiv alogia precongreso imágenes(1)
PPSX
¿Qué es Moodle?
RTF
PDF
opncc_certificate (1)
Plataformas de internet y servicios en línea
Generadores eolicos
Report Rai 3 a Expo: l’imbarazzante presenza di Coca-Cola, Ferrero e McDonald...
56 2011 tt-bgtvt
(You better) change focus, 2015 finance ict & isaca v2
2015 xiv alogia precongreso imágenes(1)
¿Qué es Moodle?
opncc_certificate (1)
Ad

Similar to Javapackages 4th semester (20)

PPTX
Java packages oop
PPT
7.Packages and Interfaces(MB).ppt .
PDF
Class notes(week 7) on packages
PPT
4.Packages_m1.ppt
PPT
packages.ppt
PPT
packages.ppt
PPT
packages in java programming language ppt
DOCX
Unit4 java
PPT
Inheritance and Polymorphism
PDF
JAVA 2-studenttrreadexeceptionpackages.pdf
PPTX
Lecture 11.pptx galgotias College of engineering and technology
DOCX
Module-4 Java Notes.docx notes about java
PPTX
Chap1 packages
PPT
Unit 4 Java
PPT
Packages(9 cm604.26)
PPT
packages unit 5 .ppt
PPTX
PPTX
Packages in java
PPTX
Java packages
PPTX
Packages
Java packages oop
7.Packages and Interfaces(MB).ppt .
Class notes(week 7) on packages
4.Packages_m1.ppt
packages.ppt
packages.ppt
packages in java programming language ppt
Unit4 java
Inheritance and Polymorphism
JAVA 2-studenttrreadexeceptionpackages.pdf
Lecture 11.pptx galgotias College of engineering and technology
Module-4 Java Notes.docx notes about java
Chap1 packages
Unit 4 Java
Packages(9 cm604.26)
packages unit 5 .ppt
Packages in java
Java packages
Packages

More from Varendra University Rajshahi-bangladesh (13)

PPTX
Computer Graphics Presentation
PPTX
Numerical method (curve fitting)
PPTX
project for web based game
PPTX
project for web based game
DOCX
Situation of women in bangladesh
PPTX
Political culture of bangladesh
PPTX
software project management
PPTX
Algorithm - Mergesort & Quicksort
PPTX
software project management
Computer Graphics Presentation
Numerical method (curve fitting)
project for web based game
project for web based game
Situation of women in bangladesh
Political culture of bangladesh
software project management
Algorithm - Mergesort & Quicksort
software project management

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Lesson notes of climatology university.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
Computing-Curriculum for Schools in Ghana
PDF
TR - Agricultural Crops Production NC III.pdf
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
Lesson notes of climatology university.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Microbial diseases, their pathogenesis and prophylaxis
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
TR - Agricultural Crops Production NC III.pdf

Javapackages 4th semester

  • 2. Packages • Package is a container for classes • A package is a grouping of related types (classes and interfaces) providing access protection and name space management. • In simple words, packages is the way we organize files into different directories according to their functionality, usability as well as category they should belong to.
  • 3. Why do we need Packages? • One can easily determine that these types are related. • One knows where to find types that can provide task- related functions. • The names of your types won't conflict with the type names in other packages because the package creates a new namespace. • One can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package.
  • 4. How To Create a Package • Packages are mirrored through directory structure. • To create a package, First we have to create a directory /directory structure that matches the package hierarchy. • Package structure should match the directory structure also. • To make a class belongs to a particular package include the package statement as the first statement of source file.
  • 10. Package access protection • Classes within a package can access classes and members declared with default access and class members declared with the protected access modifier. • Default access is enforced when neither the public, protected nor private access modifier is specified in the declaration.
  • 11. Exercise Creating Packages my package mypackageBmypackageA ABC DEG IJK XYZ S1,S2,S 3 S4,S5,S 6 A,B,CA,B,C D,E,FD,E,F A,B,C,I,J,KA,B,C,I,J,K X,Y,ZX,Y,Z Package ABC and IJK have classes with same name.Package ABC and IJK have classes with same name. A class in ABC has nameA class in ABC has name mypackage.mypackageA.ABC.A A class in IJK has nameA class in IJK has name mypackage.mypackageB.IJK.A
  • 12. How to make a class Belong to a Package • Include a proper package statement as first line in source file Make class S1 belongs to mypackageA package mypackage.mypackageA; public class S1 { public S1( ) { System.out.println("This is Class S1"); } } Name the source file as S1.java and compile it and store the S1.class file in mypackageA directory
  • 13. Make class S2 belongs to mypackageA package mypackage.mypackageA; public class S2 { public S2( ) { System.out.println("This is Class S2"); } } Name the source file as S2.javaName the source file as S2.java and compile it and store theand compile it and store the S2.class file in mypackageAS2.class file in mypackageA directorydirectory
  • 14. Make class A belongs to IJK package mypackage.mypackageB.IJK; public class A { public A( ) { System.out.println("This is Class A in IJK"); } } Name the source file as A.java and compile it and store the A.class file in IJK directory << Same Procedure For all classes>>
  • 15. Importing the PackageImporting the Package • import statement allows the importing of package • Library packages are automatically imported irrespective of the location of compiling and executing program • JRE looks at two places for user created packages (i) Under the current working directory (ii) At the location specified by CLASSPATH environment variable • Most ideal location for compiling/executing a program is immediately above the package structure.
  • 16. Example importingExample importing import mypackage.mypackageA.ABC;import mypackage.mypackageA.ABC; import mypackage.mypackageA.ABC.*;import mypackage.mypackageA.ABC.*; class packagetestclass packagetest {{ public static void main(String args[])public static void main(String args[]) {{ B b1 = new B();B b1 = new B(); C c1 = new C();C c1 = new C(); }} }} <<packagetest.java>> << Store it in location above the<< Store it in location above the package structure. Compile andpackage structure. Compile and Execute it from there>>Execute it from there>> << Store it in location above the<< Store it in location above the package structure. Compile andpackage structure. Compile and Execute it from there>>Execute it from there>>
  • 17. import mypackage.mypackageA.ABC.*; Import mypackage.mypackageB.IJK.*; class packagetest { public static void main(String args[]) { A a1 = new A(); } } << What’s Wrong Here>> << class A is present in both the imported packages ABC and IJK. So A has to be fully qualified in this case>> mypackage.mypackageA.ABC.A a1 = new mypackage.mypackageA.ABC.A(); OR mypackage.mypackageB.IJK.A a1 = new mypackage.mypackageB.IJK.A();
  • 18. Using Packages – Class in a named package can be referred to in two different ways • Using the fully qualified name packagename.ClassName • We can refer to the ElevatorPanel class in package elevator as elevator.ElevatorPlanel
  • 19. Common Mistakes • Common mistakes while running the program – The directory structure for elevator program is C:Projectelevator. – Run the program from elevator directory, and we will get the following error message c:projectelevator>java ElevatorSimulation Exception in thread "main" java.lang.NoClassDefFoundError: ElevatorSimulation – The program runs successfully by running the program from c:project directory.
  • 20. Solution • Add c:project to the CLASSPATH, and rerun the program. • The program is launched successfully without any error messages.
  • 21. Nested Classes Nested classes are defined WITHIN another class They are defined using the static keyword The nested class is defined as part of the outer class. It can only be referenced through the outer class. public class LinkedList { private static class LinkedListNode { [...] } public static class LinkedListStats {[...] }// Create an instance of the nested class LinkedListStats LinkedList.LinkedListStats x = new LinkedList.LinkedListStats(); // Illegal access to private nested class -- compiler error dLinkeList.LinkedListNode y = new LinkedList.LinkedListNode();
  • 22. Local Classes Local classes are defined within a method The scope is limited to the method Used for small helper classes whose applicability is within the method only public class LinkedList { public void traverse() { class MyIterator extends Iterator { public void doTraversal() { [...] } } MyIterator anIterator = new MyIterator(); [...] } }
  • 23. Member Classes Member classes are similar to nested classes, except they are not static. They are used in the same manner as instance variables. public class LinkedListpublic class LinkedList {{ public class LinkedListStatspublic class LinkedListStats {{ [...][...] }} LinkedList aList = new Linked List();LinkedList aList = new Linked List(); LinkedList.LinkedListStats aStat = newLinkedList.LinkedListStats aStat = new aList.LinkedListStats();aList.LinkedListStats();
  • 24. Access to members of the classes Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
  • 25. Demo:Access modifier privatepackage p1 class C1 private int x class C2 extends C1 C1 c1; c1.x cannot be read or modified package p2 class C4 extends C1 x cannot be read or modified in C2 class C5 C1 c1; c1.x cannot be read nor modifiedClass C3 C1 c1; c1.x cannot be read or modified
  • 26. Default access modifier private protected package p1 class C1 private int x class C2 extends C1 C1 c1; c1.x can be read or modified package p2 class C4 extends C1 C1 c1; c1.x can be read or modified class C5 C1 c1; c1.x cannot be read nor modifiedClass C3 C1 c1; c1.x cannot be read or modified
  • 27. Access modifier friendly package p1 class C1 private int x class C2 extends C1 C1 c1; c1.x can be read or modified package p2 class C4 extends C1 C1 c1; c1.x cannot be read nor modified class C5 C1 c1; c1.x cannot be read nor modifiedClass C3 C1 c1; c1.x can be read or modified
  • 28. Access modifier friendly package p1 class C1 private int x class C2 extends C1 C1 c1; c1.x can be read or modified package p2 class C4 extends C1 C1 c1; c1.x can be read or modified class C5 C1 c1; c1.x cannot be read nor modifiedClass C3 C1 c1; c1.x can be read or modified
  • 29. Access modifier public package p1 class C1 public int x class C3 C1 c1; c1.x can be read or modified package p2 class C2 extends C1 x can be read or modified in C2 class C4 C1 c1; c1.x can be read nor modified
  • 30.  Suppose you write a group of classes that representSuppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, andgraphic objects, such as circles, rectangles, lines, and pointspoints ●● You also write an interface, Draggable, that classesYou also write an interface, Draggable, that classes implement if they can be dragged with the mouseimplement if they can be dragged with the mouse //in the Draggable.java file//in the Draggable.java file public interface Draggable {}public interface Draggable {} //in the Graphic.java file//in the Graphic.java file public abstract class Graphic {}public abstract class Graphic {} //in the Circle.java file//in the Circle.java file public class Circle extends Graphic implements Draggable {}public class Circle extends Graphic implements Draggable {} //in the Rectangle.java file//in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { }public class Rectangle extends Graphic implements Draggable { } //in the Point.java file//in the Point.java file public class Point extends Graphic implements Draggable {}public class Point extends Graphic implements Draggable {} //in the Line.java file//in the Line.java file public class Line extends Graphic implements Draggable {}public class Line extends Graphic implements Draggable {}  Suppose you write a group of classes that representSuppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, andgraphic objects, such as circles, rectangles, lines, and pointspoints ●● You also write an interface, Draggable, that classesYou also write an interface, Draggable, that classes implement if they can be dragged with the mouseimplement if they can be dragged with the mouse //in the Draggable.java file//in the Draggable.java file public interface Draggable {}public interface Draggable {} //in the Graphic.java file//in the Graphic.java file public abstract class Graphic {}public abstract class Graphic {} //in the Circle.java file//in the Circle.java file public class Circle extends Graphic implements Draggable {}public class Circle extends Graphic implements Draggable {} //in the Rectangle.java file//in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { }public class Rectangle extends Graphic implements Draggable { } //in the Point.java file//in the Point.java file public class Point extends Graphic implements Draggable {}public class Point extends Graphic implements Draggable {} //in the Line.java file//in the Line.java file public class Line extends Graphic implements Draggable {}public class Line extends Graphic implements Draggable {} Example PackageExample Package
  • 31. Example: PlacingExample: Placing StudentRecordStudentRecord class in SchoolClassesclass in SchoolClasses pacakgepacakge package SchoolClasses;package SchoolClasses; public class StudentRecord {public class StudentRecord { private String name;private String name; private String address;private String address; private int age;private int age; ::
  • 32. Packages – Directory Paths • The CLASSPATH – List of directories and/or jar files. The compiler will look in these directories for any precompiled files it needs. – The CLASSPATH can be set as an environmental variable or specified on the command line using – classpath option. – The CLASSPATH is also used by the Java Virtual Machine to load classes.
  • 33. Compile Package Classes • Compile the program – To compile the program, we must change the working directory to the source directory root, and issue the following command c:project> javac -d . elevator*.java – By compiling everything, we get the recent version of all the classes. – Specify –d . option to tell the compiler to put the classes in a package structure starting at the root.
  • 34. Benefits of Packaging ● You and other programmers can easily determine thatYou and other programmers can easily determine that these classes and interfaces are related.these classes and interfaces are related. ● You and other programmers know where to find classesYou and other programmers know where to find classes and interfaces that can provide graphicsrelated functions.and interfaces that can provide graphicsrelated functions. ● The names of your classes and interfaces won't conflictThe names of your classes and interfaces won't conflict with the names in other packages because the packagewith the names in other packages because the package creates a new namespace.creates a new namespace. ● You can allow classes within the package to haveYou can allow classes within the package to have unrestricted access to one another yet still restrict accessunrestricted access to one another yet still restrict access for types outside the package.for types outside the package.
  • 35. THANKS TO ALLTHANKS TO ALL ID:141311056ID:141311056 ID:141311057ID:141311057 ID:141311058ID:141311058 ID:141311059ID:141311059 ID:141311060ID:141311060 ID:141311062ID:141311062 ID:141311063ID:141311063 special thanks to-special thanks to- NOYONNOYON