Top Banner
PACKAGES
12

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.

Dec 18, 2015

Download

Documents

Ann Anthony
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

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.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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 IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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 IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.

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