0% found this document useful (0 votes)
5 views13 pages

Lecture 13 Packages in java

This document provides an overview of packages in Java, explaining their purpose as a way to group related classes, interfaces, and sub-packages. It discusses the advantages of using packages, such as easy management, access protection, and code reusability, along with examples of built-in packages. Additionally, it outlines how to create, compile, and access packages in Java, including different methods for importing classes from packages.

Uploaded by

krishnathakre925
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)
5 views13 pages

Lecture 13 Packages in java

This document provides an overview of packages in Java, explaining their purpose as a way to group related classes, interfaces, and sub-packages. It discusses the advantages of using packages, such as easy management, access protection, and code reusability, along with examples of built-in packages. Additionally, it outlines how to create, compile, and access packages in Java, including different methods for importing classes from packages.

Uploaded by

krishnathakre925
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/ 13

JAVA Programming

Lecture 13: Packages in JAVA

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
April 07, 2024
Packages in JAVA

• A java package is a group of similar types of classes, interfaces and


sub-packages.
• A Java package arranges similar classes, interfaces, and sub-packages
together.
• Packages you can imagine as a Folder in OS where we put only the
related data in that folder.
• Package in java can be categorized in two form:
a. built-in package or Pre-defined Packages
b. user-defined package.
• There are many built-in packages such as java, lang, awt, javax, swing,
net, io, util, sql etc.
Advantage of Java Package
• Easy Management and Maintenance: Java package is used to
categorize the classes and interfaces so that they can be easily
maintained.
• Java package provides access protection (If we want only the classes
present in a package to access the properties of other classes of the
same package).
• Java package removes naming collision (If we want to have more than
one file with same name in different projects).
• Code Reusability: Packages and classes inside them can be imported
in other packages.
Some Pre-defined Packages in JAVA
• java.util : Utility related Classes like Scanner class is present in this package.
• Java.sql : Database connectivity related classes are present in this package.
• java.lang: We don’t need to import this package as it is implicity imported in
all our java programs
• java.io : All the classes for performing File handling in JAVA is present in
this package.
• java.awt:
• java.applet
For instance, while using the Scanner class for taking the input user, we
import the inbuilt Scanner class as import java.util.Scanner
How to Create Packages in JAVA
The package keyword is used to create a package in java.

package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Compile the Java Package
javac -d <Directory Name> <Java Source Code>

javac –d . Child.java

The -d switch specifies the destination where to put the generated


class file
How to run java package program
To Compile:
javac -d . Simple.java
To Run:
java mypack.Simple
How to access package from another package?
There are three ways to access the package from outside the package.
1. import package.*;
If you use package.*, then all the classes and interfaces of this
package will be accessible but not subpackages.
2. import package.classname;
If you import package.classname, then only declared class of this
package will be accessible.
3. 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.
1) Using packagename.*
//Save as A.java //Save as packageexample.java
package NITSRINAGAR; import NITSRINAGAR.*;
public class packageexample {
public class A { public static void main(String args[])
public void callme() {
{ A a = new A();
System.out.println("I am inside class A a.callme();
of NITSrinagar Package"); B b =new B();
} b.callme();
}
} }
2) Using packagename.classname
//Save as A.java //Save as packageexample.java
package NITSRINAGAR; import NITSRINAGAR.A;
public class packageexample {
public class A { public static void main(String args[])
public void callme() {
{ A a = new A();
System.out.println("I am inside class A a.callme();
of NITSrinagar Package"); B b =new B();
} b.callme();
}
} }
3. fully qualified name
package com.company;
public class packageexample {
public static void main(String args[])
{
NITSRINAGAR.B b =new NITSRINAGAR.B();
b.callme();
}
}
Sequence of the program
Suppose we want to create a package also, and import from some
other package also, then sequence of program must be as follows:
1. Package
2. Import
3. Class

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