packages

12
PACKAGES

Upload: nelson

Post on 22-Feb-2016

33 views

Category:

Documents


0 download

DESCRIPTION

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 All the classes and interfaces that are used for performing I/O operations can be placed in the same package - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Packages

PACKAGES

Page 2: 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

• 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

Page 3: Packages

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}

Page 4: Packages

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

<packagename>.<ClassName>– insurance.Policy

insurance.Policy policy = new insurance.Policy();

Page 5: Packages

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

Page 6: Packages

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

}}

Page 7: Packages

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

Page 8: Packages

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

Page 9: Packages

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 compilepackage 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

Page 10: Packages

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

Page 11: Packages

USES OF PACKAGES

• Logical grouping of classes and interfaces

• Avoiding clash of names

• Provides an extra level of protection to its members

Page 12: Packages

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