Presentation is loading. Please wait.

Presentation is loading. Please wait.

PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

Similar presentations


Presentation on theme: "PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces."— Presentation transcript:

1 PACKAGES

2 PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces All the classes and interfaces that are used for performing I/O operations can be placed in the same package All the classes and interfaces that are used for writing network programs can be placed in the same package

3 CREATING A PACKAGE It would be better to group all the classes and interfaces related to insurance in a package, insurance Create a folder insurance Place the following program in this folder package insurance; public class Policy{ //Code goes here }

4 CREATING A PACKAGE Compile the program –class Policy will be created in a package insurance The package statement, if existing, should be the very first statement of the file The fully qualified name of a class that is stored in a package is. –insurance.Policy insurance.Policy policy = new insurance.Policy();

5 PACKAGES TO PREVENT NAME CLASH Packages prevent the clash of class/interface names More than one class/interface can have the same name They should be in two different packages The fully qualified names of these classes/interfaces will be different

6 IMPORTING A CLASS It is difficult to use the fully qualified name of a class through out the program The import statement can be used to import a class into a program so that the class name need not be fully qualified import insurance.Policy; class Test{ public static void main(String [] args){ Policy policy = new Policy(); //Policy means insurance.Policy }

7 IMPORTING ALL CLASSES AND INTERFACES The statement import insurance.* Imports all the classes and interfaces in the package insurance All the classes and interfaces in this package can be used without qualifying them with package name

8 PACKAGES AND CLASSPATH Compiler and JVM must know location of the packages An environment variable classpath needs to be set The classpath will be pointing to a folder be one level up the package folder If the folder hierarchy is C:\work\java\insurance, the classpath should be set using the following statement >set classpath = %classpath%;C:\work\java

9 SUB PACKAGES A package can in turn contain another sub package To create a sub package policy in insurance Create a sub folder policy inside the folder insurance Place the following code in the folder policy and compile package insurance.policy; public class Policy{ //Code goes here } A class Policy will be created in a package insurance.policy –Fully qualified name will be insurance.policy.Policy

10 ACCESS MODIFIERS Java has 4 access control modifiers private: Accessible only within the class default: No keyword, Accessible only within the package protected: Similar to default with the addition that available to all child classes; that is, even if child class is in a different package public: Accessible to all Data Members and Methods can have any of these specifier Classes and Interfaces can have either the public access or the default access

11 USES OF PACKAGES Logical grouping of classes and interfaces Avoiding clash of names Provides an extra level of protection to its members

12 STANDARD JAVA PACKAGES java.lang Contains classes that form the basis of the design of the Java programming language No need to explicitly import this package The String class, System class etc, belong to this package java.io Classes and interfaces to perform I/O operations java.util Utility classes and interfaces like List, Calendar etc java.awt Classes and interfaces to create GUI


Download ppt "PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces."

Similar presentations


Ads by Google