0% found this document useful (0 votes)
13 views9 pages

Packages.docx

Packages of java

Uploaded by

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

Packages.docx

Packages of java

Uploaded by

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

Packages:

Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and
interfaces. Packages are used for:
●​ Prevent naming conflicts by allowing classes with the same name to exist in different
packages, like college.staff.cse.Employee and college.staff.ee.Employee.
●​ They make it easier to organize, locate, and use classes, interfaces, and other
components.
●​ Packages also provide controlled access for Protected members that are accessible
within the same package and by subclasses.
●​ Also for default members (no access specifier) that are accessible only within the
same package.
By grouping related classes into packages, Java promotes data encapsulation, making code
reusable and easier to manage. Simply import the desired class from a package to use it in
your program.
Working of Java Packages

Example:
import
java.util.*;
Here, util is a sub-package created inside the java package.
Simple example of java package
The package keyword is used to create a package in java.
1.​ //save as Simple.java
2.​ package mypack;
3.​ public class Simple{
4.​ public static void main(String args[]){
5.​ System.out.println("Welcome to package");
6.​ }
7.​ }
Steps:

Output:Welcome to package

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.
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but
not subpackages.
The import keyword is used to make the classes and interface of another package accessible
to the current package.
Example of package that import the packagename.*
1.​ //save by A.java
2.​
3.​ package pack;
4.​ public class A{
5.​ public void msg(){System.out.println("Hello");}
6.​ }
1.​ //save by B.java
2.​ package mypack;
3.​ import pack.*;
4.​
5.​ class B{
6.​ public static void main(String args[]){
7.​ A obj = new A();
8.​ obj.msg();
9.​ }
10.​}
Output:Hello

2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
Example of package by import package.classname
1.​ //save by A.java
2.​
3.​ package pack;
4.​ public class A{
5.​ public void msg(){System.out.println("Hello");}
6.​ }
1.​ //save by B.java
2.​ package mypack;
3.​ import pack.A;
4.​
5.​ class B{
6.​ public static void main(String args[]){
7.​ A obj = new A();
8.​ obj.msg();
9.​ }
10.​}
Output:Hello

3) Using fully qualified name


If you use fully qualified name then only declared class of this package will be accessible.
Now there is no need to import. But you need to use fully qualified name every time when
you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql
packages contain Date class.
Example of package by import fully qualified name
1.​ //save by A.java
2.​ package pack;
3.​ public class A{
4.​ public void msg(){System.out.println("Hello");}
5.​ }
1.​ //save by B.java
2.​ package mypack;
3.​ class B{
4.​ public static void main(String args[]){
5.​ pack.A obj = new pack.A();//using fully qualified name
6.​ obj.msg();
7.​ }
8.​
9.​ }
Output:Hello
Note: If you import a package, subpackages will not be imported.
If you import a package, all the classes and interface of that package will be imported
excluding the classes and interfaces of the subpackages. Hence, you need to import the
subpackage as well.

Note: Sequence of the program must be package then import then class.
Subpackage in java
Package inside the package is called the subpackage. It should be created to categorize the
package further.
Let's take an example, Sun Microsystem has definded a package named java that contains
many classes like System, String, Reader, Writer, Socket etc. These classes represent a
particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and
ServerSocket classes are for networking etc and so on. So, Sun has subcategorized the java
package into subpackages such as lang, net, io etc. and put the Input/Output related classes in
io package, Server and ServerSocket classes in net packages and so on.
The standard of defining package is domain.company.package e.g. com.javatpoint.bean or
org.sssit.dao.
Example of Subpackage
1.​ package com.javatpoint.core;
2.​ class Simple{
3.​ public static void main(String args[]){
4.​ System.out.println("Hello subpackage");
5.​ }
6.​ }

To Compile: javac -d . Simple.java

To Run: java com.javatpoint.core.Simple

Output: Hello subpackage


Types of Java Packages
●​ Built-in Packages
●​ User-defined Packages
1. Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the
commonly used built-in packages are:
●​ java.lang: Contains language support classes(e.g classes which defines primitive data
types, math operations). This package is automatically imported.
●​ java.io: Contains classes for supporting input / output operations.
●​ java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
●​ java.applet: Contains classes for creating Applets.
●​ java.awt: Contain classes for implementing the components for graphical user
interfaces (like button , ;menus etc). 6)
●​ java.net: Contain classes for supporting networking operations.
2. User-defined Packages
These are the packages that are defined by the user.
1. Create the Package:
First we create a directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being the package
names.
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;

public class MyClass


{
public void getNames(String s)
{
System.out.println(s);
}
}
Static Import In Java
Static Import in Java is about simplifying access to static members and separates it from the
broader discussion of user-defined packages.
Static import is a feature introduced in Java programming language (versions 5 and above)
that allows members (fields and methods) defined in a class as public static to be used in
Java code without specifying the class in which the field is defined.
Example:
// Note static keyword after import.
import static java.lang.System.*;

class Student {
public static void main(String args[]) {

// We don't need to use 'System.out'


// as imported using static.
out.println("Hello");
}
}
Output
Hello
Handling Name Conflicts
When two packages contain a class with the same name (e.g., java.util.Date and
java.sql.Date), specify the full package name to avoid conflicts.
import java.util.*;​
import java.sql.*;​

//And then use Date class, then we will get a compile-time error :​

Date today ; //ERROR– java.util.Date or java.sql.Date?
The compiler will not be able to figure out which Date class do we want. This problem can be
solved by using a specific import statement:
import java.util.Date;​
import java.sql.*;
If we need both Date classes then, we need to use a full package name every time we declare
a new object of that class. For Example:
java.util.Date deadLine = new java.util.Date();

java.sql.Date today = new java.sql.Date();
Illustration of user-defined packages: Creating our first package: File name –
ClassOne.java
package package_name;

public class ClassOne {


public void methodClassOne()
{
System.out.println("Hello there its ClassOne");
}
}
Creating our second package: File name – ClassTwo.java
package package_one;

public class ClassTwo {


public void methodClassTwo()
{
System.out.println("Hello there i am ClassTwo");
}
}
Making use of both the created packages: File name – Testing.java
import package_name.ClassOne;
import package_one.ClassTwo;

public class Testing {


public static void main(String[] args)
{
ClassTwo a = new ClassTwo();
ClassOne b = new ClassOne();
a.methodClassTwo();
b.methodClassOne();
}
}
Now having a look at the directory structure of both the packages and the testing class file:

Access Modifiers in the Context of Packages


1.​ Public: Members with the public modifier are accessible from anywhere, regardless
of whether the accessing class is in the same package or not.
2.​ Protected: Memebers with the protected modifier are accessible within the same
package, In subclasses
3.​ Default: Members with no modifier are accessible only within the same package
4.​ Private: Members with the private modifier are accessible only within the same class.
They cannot be accessed by classes in the same package, subclasses, or different
packages.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy