0% found this document useful (0 votes)
26 views7 pages

Oop Present

User-defined packages in Java allow users to create their own packages that can be imported and used like built-in packages. To create a user-defined package, the package name is declared using the package keyword followed by the class definition. Classes within a package are accessed either using a fully qualified class name or by importing the package. Packages organize related classes and interfaces, prevent accidental deletion of classes, isolate classes from other packages to allow name reuse, and promote reusability through libraries of packages.

Uploaded by

Robin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Oop Present

User-defined packages in Java allow users to create their own packages that can be imported and used like built-in packages. To create a user-defined package, the package name is declared using the package keyword followed by the class definition. Classes within a package are accessed either using a fully qualified class name or by importing the package. Packages organize related classes and interfaces, prevent accidental deletion of classes, isolate classes from other packages to allow name reuse, and promote reusability through libraries of packages.

Uploaded by

Robin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

USER DEFINED PACKAGES

The users of the Java language can also create their own packages.
They are called user-defined packages. User defined packages can
also be imported into other classes & used exactly in the same way as
the Built in packages.

i) Creating User Defined Packages


Syntax :
package packageName;
public class className
{
- - - - - - - - - - - - -
// Body of className
- - - - - - - - - - - -
}

We must first declare the name of the package using the package
keyword followed by the package name. This must be the first
statement in a Java source file. Then define a classes as normally as
define a class.
Example :
package myPackage;
public class class1
{
- - - - - - - - - - - - -
// Body of class1
}
How to use this package in another program.
ACCESSING A PACKAGE
Java package can be accessed either using a fully qualified
class name or using a shortcut approach through the import
statement.
Syntax :
import package1[.package2][.package3].classname;

Here, package1 is the name of the top level package, package2


is the name of the package that is inside the package & so on.
We can have any number of packages in a package hierarchy.
Finally the explicit classname is specified. The import statement
must end with a semicolon (;). The import startment should
appear before any class definitions in a source file. Multiple
import statements are allowed.
Ex :
import firstpackage.secondPackage.Myclass;
or
import firstpackage.*;
ADVANTAGES OF PACKAGES
There are several advantages of package some of them are as
follow :-
1: Packages are useful to arrange related classes and interface
into a group.This makes all the classes & interface performing
the same task to put together in the same package.
2: Packages hide the classes & interfaces in a seprate
subdirectory, so that accidental deletion of classes & interfaces
will not take place.
3: The classes & interfaces of a packages are isolated form the
classes & interfaces of another packages. This means that we
can use same names for classes of two different classes.
4: A group of packages is called a library. The classes & interface
of a package are like books in a library & can be reused
several times. This reusability nature of packages makes
programming easy.

You might also like