0% found this document useful (0 votes)
4 views7 pages

Java Package Basics and Examples

The document provides an overview of packages in Java, defining them as namespaces that organize classes and interfaces to avoid name conflicts and enhance modularity. It outlines the syntax for creating packages, rules for their implementation, and includes multiple examples demonstrating package creation, access, and usage of subpackages. Additionally, it explains the use of access modifiers within packages to control visibility and access to classes and methods.

Uploaded by

kmd34393
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)
4 views7 pages

Java Package Basics and Examples

The document provides an overview of packages in Java, defining them as namespaces that organize classes and interfaces to avoid name conflicts and enhance modularity. It outlines the syntax for creating packages, rules for their implementation, and includes multiple examples demonstrating package creation, access, and usage of subpackages. Additionally, it explains the use of access modifiers within packages to control visibility and access to classes and methods.

Uploaded by

kmd34393
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

Introduction to Packages

Definition:
A package in Java is a namespace that organizes classes and
interfaces.

Purpose:
- Avoids name conflicts
- Provides access control
- Makes code modular and reusable

Types of Packages:
- Built-in Packages (e.g., [Link], [Link])
- User-defined Packages (created by developers)
Syntax for Creating a Package
Syntax:
package package_name;
public class ClassName {
// class body
}

Rules:
- Must be the first statement in the file.
- The source file should be placed in a folder matching the
package name.
- Compile using: javac -d . [Link]
- Run using: java package_name.ClassName
Example 1 – Create a Simple Package
File: MyPackage/[Link]

package MyPackage;

public class Demo {


public void display() {
[Link]("Hello from MyPackage!");
}
}

Compile & Run:


- javac -d . [Link]
- java [Link]
Example 2 – Access Package Class from
Another File
File 1: MyPackage/[Link]
package MyPackage;
public class Demo {
public void show() {
[Link]("Inside MyPackage Demo class");
}
}
File 2: [Link]
import [Link];

public class TestPackage {


public static void main(String[] args) {
Demo obj = new Demo();
[Link]();
}
}
Example 3 – Package with Multiple Classes
File 1: mypack/[Link]
File 3: [Link]
package mypack;
public class Addition { import mypack.*;
public int add(int a, int b) {
public class MainTest {
return a + b;
public static void main(String[] args) {
} Addition add = new Addition();
} Subtraction sub = new Subtraction();
[Link]("Sum: " + [Link](10, 5));
File 2: mypack/[Link]
[Link]("Difference: " + [Link](10, 5));
}
package mypack; }
public class Subtraction {
public int sub(int a, int b) {
return a - b;
}
}
Example 4 – Subpackages in Java
File: university/students/[Link]
package [Link];
public class Info {
public void show() {
[Link]("Welcome to University Students Package");
}
}
File: [Link]
import [Link];

public class Main {


public static void main(String[] args) {
Info obj = new Info();
[Link]();
}
}
Example 5 – Package + Access Modifiers
File: college/[Link]
package college;
public class Teacher {
protected void display() {
[Link]("Protected method from college package");
}
}
File: [Link]
import [Link];
public class MainApp extends Teacher {
public static void main(String[] args) {
MainApp obj = new MainApp();
[Link]();
}
}

You might also like