Skip to content

Commit e989d86

Browse files
committed
Section 17 Packages
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9393e28 commit e989d86

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Section17Packages/src/Packages.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Java Packages
2+
3+
A **package** in Java is a way to group related classes, interfaces, and sub-packages.
4+
It helps in **organizing code**, **avoiding name conflicts**, and **controlling access**.
5+
6+
## Types of Packages
7+
1. **Built-in packages** → Provided by Java (e.g., `java.util`, `java.io`, `java.sql`).
8+
2. **User-defined packages** → Created by developers to organize their own code.
9+
10+
## How to Create a Package
11+
12+
```java
13+
// File: MyClass.java
14+
package mypack;
15+
16+
public class MyClass {
17+
public void show() {
18+
System.out.println("Hello from mypack!");
19+
}
20+
}
21+
```
22+
23+
Compile with:
24+
25+
javac -d . MyClass.java
26+
27+
Run with:
28+
29+
java mypack.MyClass
30+
31+
Benefits
32+
• Better code organization
33+
• Reusability of classes
34+
• Encapsulation and access control
35+
• Avoids class name collisions

0 commit comments

Comments
 (0)