0% found this document useful (0 votes)
2 views17 pages

9e Packages (1)

The document provides an overview of Java packages, explaining their purpose in organizing classes and interfaces to avoid name conflicts and enhance code management. It details the steps for creating user-defined packages, the structure of built-in packages, and the visibility of class members across different packages. Additionally, it highlights the advantages of using packages, such as code organization, encapsulation, and reusability, along with examples of using various Java API packages.

Uploaded by

kiritokev21
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 views17 pages

9e Packages (1)

The document provides an overview of Java packages, explaining their purpose in organizing classes and interfaces to avoid name conflicts and enhance code management. It details the steps for creating user-defined packages, the structure of built-in packages, and the visibility of class members across different packages. Additionally, it highlights the advantages of using packages, such as code organization, encapsulation, and reusability, along with examples of using various Java API packages.

Uploaded by

kiritokev21
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/ 17

Object Oriented

Paradigm

Topic: Packages
Packages
• A package in Java is a namespace that organizes classes and
interfaces. It helps in avoiding name conflicts and managing
large codebases efficiently.
•A package is a collection of related Java classes and interfaces.
•It acts as a container that groups similar types of classes,
interfaces, and sub-packages.
•Helps in modularization, code reusability, and avoiding class
name conflicts.
•Packages can be built-in (Java API packages) or user-defined.
Packag
es
Packages
Built-in User-defined
Packages Packages
Predefined packages that come as a part of JVM to
simplify the task of Java programmer.
Eg: java.lang, java.io, java.util,
java.applet, java.sql
java Package

lang util awt Sub-Packages

System.cl String.clas Arraylist.cla Classes


Map.class Button.class
ass s ss
Creating Packages
package
MyPackage;
The package statement simply specifies to which package the classes
defined belongs to..

Including class in JAVA package


Declare the package name as the first statement of your
program.
Then include the class as part of the package. But, remember
that, a class can have only one package declaration.
Built-in Packages
import java.util.Scanner;;

User defined Packages


User-defined packages are those which are
developed by users in order to group related
classes, interfaces and sub packages.
Creating a Package (Step-by-Step)
Step 1: Declare the Package
 Use the package keyword at the beginning of the Java file.

Step 2: Define the Classes


 Classes inside the package must be stored in the corresponding directory structure.

Step 3: Compile the Package


 Use javac -d . ClassName.java to compile and store the package properly.
 -d  creates the mypackage directory and places the compiled .class file inside.

Step 4: Use the Package in Another Class


 Import it using import packageName.*; or import packageName.ClassName
Execution Compiles the
package

javac -d directory .
This forces the compiler to create the
javafilename
“MyPackage" package.
The -d keyword specifies the destination for
where to save the class file.
javac -d.filename.java You can use any directory name, like c:/user
(windows), or, if you want to keep the
package within the same directory, you can
use the dot sign ".",
To run:

java MyPackage.filename

7
package FirstProgramPackage;
public static void main(String args[]) {
public class CSEAB {
CSEAB bright = new CSEAB(5,10);
int num1, num2;
bright.getmax();
CSEAB(int n, int m) {
}
num1 = n;
}
num2 = m;
}
public void getmax(){
if ( num1 > num2 ) {
System.out.println("Maximum value of two
numbers is " + num1);
}
else { Maximum value of two numbers is 10
System.out.println("Maximum value of two
numbers is " + num2);
}
}
package SecondProgram Package;
import FirstProgramPackage.CSEAB;

public class Demo{


public static void main(String args[]) {
//Using fully qualified name instead of import
int n=10, m=10;
CSEAB bright = new CSEAB(n, m); FirstProgramPackage.CSEAB bright = new FirstProgramPackage.CSEAB(n,

if(n != m) { m);

current.getmax();
}
else {
System.out.println("Both the values are Both the values are same
same");
}
}
}
Java packages addresses four categories
of visibility for class members:
• Sub-classes in the same package
• Non-subclasses in the same package
• Sub-classes in different packages
• Classes that are neither in the same package nor
sub-classes
Protecte
Private Default Public
d
Same Class Yes Yes Yes Yes
Same Package Subclasses No Yes Yes Yes
Same Package Non-Subclasses No Yes Yes Yes
Different Packages Subclasses No No Yes Yes
Different Packages Non-
No No No Yes
Subclasses
Sub-packages in Java
• A package inside another package is called a sub-package.
Example: mypackage.subpackage
package mypackage.subpackage;
public class SubClass {
public void show() {
System.out.println("Inside SubPackage Class"); }}
Using the SubPackage Class:
import mypackage.subpackage.SubClass;
public class TestSubPackage {
public static void main(String args[]) {
SubClass obj = new SubClass();
obj.show(); }}
Advantages of Using Packages
Code Organization: Groups related classes together.
Encapsulation: Controls class access using access modifiers.
Avoids Name Conflicts: Two different packages can have classes with the same name.
Code Reusability: Existing packages can be reused in multiple projects.
Java API Packages

Package Name Description


java.lang Contains core classes (Math, String, Object)
java.util Utility classes (Collections, Date, Scanner)
java.io Input/output operations (File handling)
java.sql Database operations (JDBC)
java.net Networking functionalities
Using java.lang Package
The java.lang package contains fundamental classes such as String, Math, Integer, System,
etc.
No need to import this package explicitly as it's automatically available.
public class LangExample {
public static void main(String args[]) {
double result = Math.sqrt(25); // Using Math class
System.out.println("Square root of 25: " + result);
String str = "Hello, Java!"; // Using String class
System.out.println("String_Length: " + str.length()); }}
Output:
Square root of 25: 5.0
String Length: ?
12
Using java.util Package
The java.util package contains utility classes for handling data structures, date, random numbers, etc.
import java.util.ArrayList;  Q1: Change it to integer values list
import java.util.Date;
Q2. Covert String to Integer
public class UtilExample {
public static void main(String args[]) {
Date today = new Date(); // Using Date class  Ans 1: replace String to Integer &
System.out.println("Current Date: " + today); Values as number & remove double
ArrayList<String> list = new ArrayList<>(); // Using ArrayList colons simply numberwithin brace
list.add("Apple"); Ans 2: Convert the strings to integers usin
list.add("Banana"); Integer.valueOf()
list.add("Cherry");  list.add(Integer.valueOf("1"));
System.out.println("Fruits List: " + list); }}  list.add(Integer.valueOf("2"));
Output:
 list.add(Integer.valueOf("3"));
• Current Date: Tue Mar 18 14:30:00 IST 2025
• Fruits List: [Apple, Banana, Cherry]
Using java.net Package- Fetching a webpage using URL
import java.net.URL;
🔹 Output (Depends on the
import java.util.Scanner;
webpage content):
public class NetworkExample {
php-template
public static void main(String args[]) {
CopyEdit
try {
URL url = new URL("http://example.com"); <!DOCTYPE html>

Scanner sc = new Scanner(url.openStream()); <html>


while (sc.hasNext()) { <head><title>Example
Domain</title></head>
System.out.println(sc.nextLine()); }
sc.close(); <body>...</body>

} catch (Exception e) { </html>


System.out.println("Error fetching URL."); } }}
 URL class reads content from a website.
 Scanner fetches webpage content.
THANK YOU….

You might also like