Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 Packages

2 CSI 3125, Preliminaries, page 2 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

3 CSI 3125, Preliminaries, page 3 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.

4 CSI 3125, Preliminaries, page 4 Packages

5 CSI 3125, Preliminaries, page 5 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

6 CSI 3125, Preliminaries, page 6 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 MyPackage. package MyPackage; Java uses file system directories to store packages. For example, the.class files for any classes declare to be part of MyPackage must be stored in a directory called MyPackage. Directory name must match the package name exactly.

7 CSI 3125, Preliminaries, page 7 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

8 CSI 3125, Preliminaries, page 8 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

9 CSI 3125, Preliminaries, page 9 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

10 CSI 3125, Preliminaries, page 10 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.

11 CSI 3125, Preliminaries, page 11 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.*;

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

13 CSI 3125, Preliminaries, page 13 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

14 CSI 3125, Preliminaries, page 14 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 ServerSocket 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 ServerSocket classes in net packages and so on. import java.io.*; import java.lang.*;

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

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

17 CSI 3125, Preliminaries, page 17 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.

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

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

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

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


Download ppt "CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages."

Similar presentations


Ads by Google