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

Java Packages Unit-1

The document provides an overview of Java packages, explaining their purpose in organizing classes and interfaces based on functionality. It discusses the advantages of using packages, such as reusability, name conflict resolution, and data encapsulation, and distinguishes between built-in and user-defined packages. Additionally, it outlines methods for accessing packages from outside, including import statements and using fully qualified names.

Uploaded by

Neha Sharma
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)
2 views

Java Packages Unit-1

The document provides an overview of Java packages, explaining their purpose in organizing classes and interfaces based on functionality. It discusses the advantages of using packages, such as reusability, name conflict resolution, and data encapsulation, and distinguishes between built-in and user-defined packages. Additionally, it outlines methods for accessing packages from outside, including import statements and using fully qualified names.

Uploaded by

Neha Sharma
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/ 15

Java Packages

Rita Nagar
Assistant Professor, CSE Department MPGC
Outlines
Creating and Accessing packages

Access specifiers

This pointer

CLASSPATH Setting for Packages


Creating and Accessing packages
Packages
Java package is a mechanism of grouping similar type of classes,
interfaces, and sub-classes collectively based on functionality. Java
programming language composed of hundreds or even thousands
of individual classes. It makes sense to keep things organized by
placing related classes and interfaces into packages.

Conceptually, you can think of java packages as


being similar to different folders on your
computer.

Whichever class we are using in JAVA


belongs to a package.

Object Oriented Programming in


Advantantages of java package
• Re-usability: The classes contained in the packages of another
program can be easily reused.
• Name Conflicts: Packages help us to uniquely identify a class.
• Controlled Access: Offers access protection such as protected
classes, default classes and private class
• Data Encapsulation: They provide a way to hide classes,
preventing other programs from accessing classes that are meant
for internal use only.
• Maintenance: With packages, you can organize your project better
and easily locate related classes
Object Oriented Programming in
Types of Packages in Java

Packages

Built-in Packages User Defined


Packages

Object Oriented Programming in


Built-in Packages

Built-in packages or predefined packages are those that come


along as a part of JDK (Java Development Kit) to simplify the
task of Java programmer. They consist of a huge number of
predefined classes and interfaces that are a part of Java API’s.

Some of the commonly used built-in packages are java.lang, java.io,


java.util, java.applet, etc. Here’s a simple program using a built-in
package.

Object Oriented Programming in


Example

Java nextLine() method


The nextLine() method of Scanner class is used to take a string from the user. It is
defined in java.util.Scanner class. The nextLine() method reads the text until the end of
the line

Object Oriented Programming in


Output

Object Oriented Programming in


User-defined packages
User-defined packages are those which are developed by users in
order to group related classes, interfaces and sub packages

user-defined packages in Java are essentially packages that are


defined by the programmer.

Whenever we want to add a class to the package, it can be


mentioned by the package name and the “package” keyword at the
top of the program.

keyword to define package “package”

Object Oriented Programming in


How to access package from another package?

There are three ways to access the package from outside the

package.

1. import package.*;

2. import package.classname;

3. fully qualified name


import package.*;

Output : Hello
import package.classname;

Output : Hello
Example: fully qualified name

Object Oriented Programming in


Output
Compile the java files in this way --

• User need to put both the files on the same location


• As we wanted to use the file A.java in B.java
• First we need to compile A.java, then compile B.java
• Second , run the B,java code only that uses the A.java.

Object Oriented Programming in

You might also like