0% found this document useful (0 votes)
44 views

A Package Is A Collection of Similar Types of Classes

Packages in Java are used to organize related classes and interfaces by subject. The purpose of packages is to avoid naming collisions and provide access protection. There are predefined packages that are part of the Java API and user-defined packages that are created by programmers. Package programs contain common classes and interfaces but cannot be executed on their own - they must be imported and used within other programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

A Package Is A Collection of Similar Types of Classes

Packages in Java are used to organize related classes and interfaces by subject. The purpose of packages is to avoid naming collisions and provide access protection. There are predefined packages that are part of the Java API and user-defined packages that are created by programmers. Package programs contain common classes and interfaces but cannot be executed on their own - they must be imported and used within other programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

A package is a collection of similar types of classes, interfaces and sub-packages.

Purpose of package

The purpose of package concept is to provide common classes and interfaces for any program separately. In

other words if we want to develop any class or interface which is common for most of the java programs than

such common classes and interfaces must be place in a package.

Packages in Java are the way to organize files when a project has many modules. Same like we organized our

files in Computer. For example we store all movies in one folder and songs in other folder, here also we store

same type of files in a particular package for example in awt package have all classes and interfaces for design

GUI components.
Advantage of package

 Package is used to categorize the classes and interfaces so that they can be easily maintained

 Application development time is less, because reuse the code

 Application memory space is less (main memory)

 Application execution time is less

 Application performance is enhance (improve)

 Redundancy (repetition) of code is minimized

 Package provides access protection.

 Package removes naming collision.

Type of package

Package are classified into two type which are given below.

1. Predefined or built-in package

2. User defined package

Predefined or built-in package


These are the package which are already designed by the Sun Microsystem and supply as a part of java API,

every predefined package is collection of predefined classes, interfaces and sub-package.

User defined package

If any package is design by the user is known as user defined package. User defined package are those which

are developed by java programmer and supply as a part of their project to deal with common requirement.

Rules to create user defined package

 package statement should be the first statement of any package program.

 Choose an appropriate class name or interface name and whose modifier must be public.

 Any package program can contain only one public class or only one public interface but it can contain

any number of normal classes.

 Package program should not contain any main class (that means it should not contain any main())

 modifier of constructor of the class which is present in the package must be public. (This is not

applicable in case of interface because interface have no constructor.)

 The modifier of method of class or interface which is present in the package must be public (This rule

is optional in case of interface because interface methods by default public)

 Every package program should be save either with public class name or public Interface name
Compile package programs

For compilation of package program first we save program with public className.java and it compile using

below syntax:

Syntax

javac -d . className.java

Syntax

javac -d path className.java

Explanations: In above syntax "-d" is a specific tool which is tell to java compiler create a separate folder for

the given package in given path. When we give specific path then it create a new folder at that location and

when we use . (dot) then it crate a folder at current working directory.


Note: Any package program can be compile but can not be execute or run. These program can be executed

through user defined program which are importing package program.

Example of package program

Package program which is save with A.java and compile by javac -d . A.java

Example

package mypack;

public class A

public void show()

System.out.println("Sum method");

Import above class in below program using import packageName.className

Example

import mypack.A;

public class Hello

public static void main(String arg[])

A a=new A();

a.show();

System.out.println("show() class A");


}

Explanations: In the above program first we create Package program which is save with A.java and compiled

by "javac -d . A.java". Again we import class "A" in class Hello using "import mypack.A;" statement.

You might also like