Packages CSI 3125 Preliminaries page 1 Packages Packages

  • Slides: 21
Download presentation
Packages CSI 3125, Preliminaries, page 1

Packages CSI 3125, Preliminaries, page 1

Packages • Packages are containers for classes • Collection of classes • Packages are

Packages • Packages are containers for classes • Collection of classes • Packages are stored in a hierarchical manner and are explicitly imported into new class definitions • used for each class to avoid name collisions. • Java provides a mechanism for partitioning the class name space into more manageable chunks. • This mechanism is the package CSI 3125, Preliminaries, page 2

Packages • Classes can be defined inside a package that are not accessible by

Packages • Classes can be defined inside a package that are not accessible by code outside that package. • Also define class members • Advantages • 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. • 2) Java package provides access protection. • 3) Java package removes naming collision. CSI 3125, Preliminaries, page 3

Packages CSI 3125, Preliminaries, page 4

Packages CSI 3125, Preliminaries, page 4

Defining a Package • Include a package command as the first statement in a

Defining a Package • Include a package command as the first statement in a Java source file. • Any classes declared within that file will belong to the specified package. • The package statement defines a name space in which classes are stored. • If omit the package statement, the class names are put into the default package, which has no name CSI 3125, Preliminaries, page 5

Defining a Package General form of the package statement: package pkg; Here, pkg is

Defining a Package General form of the package statement: package pkg; Here, pkg is the name of the package. For example, the following statement creates a package called My. Package. package My. Package; Java uses file system directories to store packages. For example, the. class files for any classes declare to be part of My. Package must be stored in a directory called • My. Package. • Directory name must match the package name exactly. • • CSI 3125, Preliminaries, page 6

Defining a Package • The class in the package must be public • The

Defining a Package • The class in the package must be public • The methods in the class in the package also be public • Store in a sub folder with the name of the package CSI 3125, Preliminaries, page 7

Defining a Package • Example • Create a folder pack • Create the following

Defining a Package • Example • Create a folder pack • Create the following code and store as A. java in pack folder • • • package pack; public class A { public void msg(){System. out. println("Hello"); } } • Compile as A. java to create A. class CSI 3125, Preliminaries, page 8

Defining a Package • Example • Create the following code and store as B.

Defining a Package • Example • Create the following code and store as B. java in the parent folder • • • import pack. *; class B{ public static void main(String args[]){ A obj = new A(); obj. msg(); //object of package class A } } • Compile and run the code B. java CSI 3125, Preliminaries, page 9

Defining a Package • There are three ways to access the package from outside

Defining a Package • There are three ways to access the package from outside the package. • 1) import package. *; • 2) import package. classname; • 3) fully qualified name. CSI 3125, Preliminaries, page 10

Defining a Package • 1) Using packagename. * • If use package. * then

Defining a Package • 1) Using packagename. * • If use package. * then all the classes and interfaces of this package will be accessible but not subpackages. • import pack. *; CSI 3125, Preliminaries, page 11

Defining a Package • 2) Using packagename. classname • If import package. classname then

Defining a Package • 2) Using packagename. classname • If import package. classname then only declared class of this package will be accessible. • import pack. A; CSI 3125, Preliminaries, page 12

Defining a Package • 3) Using fully qualified name • If use fully qualified

Defining a Package • 3) Using fully qualified name • If use fully qualified name then only declared class of this package will be accessible. • Now there is no need to import. • But you need to use fully qualified name every time when you are accessing the class or interface. • pack. A obj = new pack. A(); • Where A is in the pack folder • when two packages have same class name e. g. java. util and java. sql packages contain Date class CSI 3125, Preliminaries, page 13

Subpackage in java • Package inside the package is called the subpackage. • Example,

Subpackage in java • Package inside the package is called the subpackage. • Example, Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc. • These classes represent a particular group e. g. • Reader and Writer classes are for Input/Output operation, Socket and Server. Socket classes are for networking etc and so on. • So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. • And put the Input/Output related classes in io package, Server and Server. Socket classes in net packages and so on. • import java. io. *; • import java. lang. *; CSI 3125, Preliminaries, page 14

Subpackage in java • • • • //save as A. java in the folder

Subpackage in java • • • • //save as A. java in the folder p 1 package p 1; public class A { public void show_a(){ System. out. println("show a"); } } //save B. java in the folder p 1/p 2 package p 1. p 2; public class B { public void show_b(){ System. out. println("show b"); } } CSI 3125, Preliminaries, page 15

Subpackage in java • • • • // to access the package in p

Subpackage in java • • • • // to access the package in p 1 & P 1/P 2 //Save as pack in root folder import p 1. *; // for accessing the class A in p 1 folder import p 1. p 2. *; // for accessing the class B in p 1/p 2 class pack { public static void main(String[] args) { System. out. println("Hello World!"); B obj=new B(); obj. show_b(); A obj 1=new A(); obj 1. show_a(); } } CSI 3125, Preliminaries, page 16

How to put two public classes in a package? • There can be only

How to put two public classes in a package? • There can be only one public class in a java source file and it must be saved by the public class name. • If you want to put two public classes in a package, have two java source files containing one public class, but keep the package name same. CSI 3125, Preliminaries, page 17

How to put two public classes in a package? • • • //save as

How to put two public classes in a package? • • • //save as A. java in pack folder package pack; public class A{ public void msg 1(){System. out. println(“MSG 1"); } } //save as B. java in pack folder package pack; public class B{ public void msg 2(){System. out. println(“MSG 2"); } } CSI 3125, Preliminaries, page 18

How to put two public classes in a package? • • • import pack.

How to put two public classes in a package? • • • import pack. *; class C{ public static void main(String args[]){ A obj 1=new A(); obj 1. msg 1(); B obj 2=new B(); obj 2. msg 2(); } } CSI 3125, Preliminaries, page 19

Defining a Package • Classes CSI 3125, Preliminaries, page 20

Defining a Package • Classes CSI 3125, Preliminaries, page 20

Defining a Package • Classes CSI 3125, Preliminaries, page 21

Defining a Package • Classes CSI 3125, Preliminaries, page 21