[go: up one dir, main page]

0% found this document useful (0 votes)
10 views22 pages

Packages

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)
10 views22 pages

Packages

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/ 22

Packages

Introduction

• Java packages are used to organize classes and


interfaces.
• Helps avoid class name conflicts and makes
code modular.
• We will cover package creation, setting
CLASSPATH, access control, and importing
packages.
What is a Package?

• A package is a namespace that organizes


classes and interfaces.
• Similar to folders in an operating system.
Example:
package mypackage;
public class MyClass {
public void display() {
System.out.println("Hello from MyClass");
}
}
Creating a Package
• Define a package using the package keyword.
• Store the class inside a corresponding directory structure.
• Example:
package utilities;
public class Utility {
public static void printMessage() {
System.out.println("Utility message");
}
}

Compiling and Running a Package


• Compile: javac -d . Utility.java
• Run: java utilities.Utility
Setting CLASSPATH
• CLASSPATH tells JVM where to look for classes.
• Can be set using the -cp option or
environment variable.
• Example:
export CLASSPATH=/path/to/classes
Creating User-Defined Packages with Math Operations

• We can create a package for mathematical operations.


• Example:
package mathoperations;
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
}
Using the MathOperations Package

• Import the user-defined package and use it.


• Example:
import mathoperations.MathUtils;
public class Main {
public static void main(String[] args) {
System.out.println("Addition: " + MathUtils.add(5, 3));
System.out.println("Subtraction: " + MathUtils.subtract(10, 4));
}
}
Creating a Package with Multiple Classes

• A package can have multiple classes.


• Example:
package shapes;
public class Circle {
public double area(double radius) {
return Math.PI * radius * radius;
}
}
public class Rectangle {
public int area(int length, int breadth) {
return length * breadth;
}
}
Using the Shapes Package
import shapes.Circle;
import shapes.Rectangle;
public class Demo {
public static void main(String[] args) {
Circle c = new Circle();
Rectangle r = new Rectangle();
System.out.println("Circle Area: " + c.area(5));
System.out.println("Rectangle Area: " + r.area(4, 6));
}
}
Access Control Modifiers

• public: Accessible from anywhere.


• protected: Accessible within package and
subclasses.
• default: Accessible only within package.
• private: Accessible only within class.
• Public Access Modifier • Private Access Modifier
Example Example
package example; package example;
public class PublicClass { public
class PrivateExample
void show()
{ System.out.println("Public { private void show() {
method"); System.out.println("Pri
} vate method");
} }
}
Protected Access Modifier Example Default Access Modifier Example
package example;
public class ProtectedExample{ package example;
protected void show() class DefaultExample {
{ System.out.println("Protect
void show()
ed method");
{ System.out.println("Default
} method");
} }
}
Importing Packages

• Use import keyword to use classes from


another package.
• Example:
import utilities.Utility;
class Main {
public static void main(String args[]) {
Utility.printMessage();
}
}
Importing All Classes

• Use wildcard * to import all classes in a package.

import utilities.*;

Static Import
• Import static members of a class directly.
• Example:
import static utilities.Utility.printMessage;
class Main {
public static void main(String args[]) { printMessage();
}
}
Creating and Using a Package
• This example defines a package named mypackage and a
class inside it.
Step 1: Create a package and class (Save as MyClass.java)
// Package declaration
package mypackage;

public class MyClass {


public void displayMessage() {
System.out.println("Hello from MyClass in
mypackage!");
}
}
Step 2: Use the package in another Java program (Save as
TestPackage.java)

// Importing the package


import mypackage.MyClass;

public class TestPackage {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayMessage(); // Calling the method
}
}
Compilation and Execution:
javac -d . MyClass.java
javac -d . TestPackage.java
java TestPackage
2. Using Multiple Classes in a Package
Step 1: Define multiple classes inside a package (mypackage)

// File: Message.java
package mypackage;

public class Message {


public void showMessage() {
System.out.println("This is a message from the mypackage.");
}
}

// File: Greetings.java
package mypackage;

public class Greetings {


public void greet() {
System.out.println("Hello! Greetings from mypackage.");
}
}
Step 2: Access both classes in another program
// File: PackageDemo.java
import mypackage.Message;
import mypackage.Greetings;

public class PackageDemo {


public static void main(String[] args) {
Message msg = new Message();
Greetings greet = new Greetings();

msg.showMessage();
greet.greet();
}
}

javac -d . Message.java Greetings.java


javac -d . PackageDemo.java
java PackageDemo
3. Using Sub-Packages
Step 1: Create a subpackage (mypackage.subpackage)

// File: SubPackageClass.java
package mypackage.subpackage;

public class SubPackageClass {


public void display() {
System.out.println("Inside the subpackage:
mypackage.subpackage");
}
}
Step 2: Access the subpackage in another program

// File: SubPackageDemo.java
import mypackage.subpackage.SubPackageClass;

public class SubPackageDemo {


public static void main(String[] args) {
SubPackageClass obj = new SubPackageClass();
obj.display();
}
}
Compilation and Execution:
javac -d . SubPackageClass.java
javac -d . SubPackageDemo.java
java SubPackageDemo
4. Using static import for accessing static members
// File: MathUtil.java
package mypackage;

public class MathUtil {


public static int square(int x) {
return x * x;
}
}

// File: StaticImportDemo.java
import static mypackage.MathUtil.square;

public class StaticImportDemo {


public static void main(String[] args) {
int num = 5;
System.out.println("Square of " + num + " is: " + square(num));
}
}
Compilation and Execution:
javac -d . MathUtil.java
javac -d . StaticImportDemo.java
java StaticImportDemo
Conclusion

• Java packages help in organizing code.


• CLASSPATH ensures JVM finds the right
classes.
• Access modifiers control class visibility.
• Importing packages allows code reuse.

You might also like