0% found this document useful (0 votes)
2 views

Unit 4 Packages in Java.pptx

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined packages. Benefits of using packages include access protection and avoidance of naming collisions. The document also explains how to create, compile, and access user-defined packages in Java with examples.

Uploaded by

newmovies1638
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)
2 views

Unit 4 Packages in Java.pptx

A Java package is a collection of related classes, interfaces, and sub-packages, categorized into built-in and user-defined packages. Benefits of using packages include access protection and avoidance of naming collisions. The document also explains how to create, compile, and access user-defined packages in Java with examples.

Uploaded by

newmovies1638
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

Packages in Java

Unit 3
What is a Package?
• A java package is a group of similar types of classes, interfaces and
sub-packages.
• Package in java can be categorized in two form, built-in package and
user-defined package.
• There are many built-in packages such as java, lang, awt, javax, swing,
net, io, util, sql etc.
• User defined package are allows you to create packages as per your
need.
Benefits of using package

Java package provides access protection.


Java package removes naming collision.
Java API Packages
Using System packages
Naming conventions
Creating user defined packages
To define a package in Java, you use the keyword package.

package packageName;
Example:
package letmecalculate;
public class Calculator {
public int add(int a, int b){
return a+b;
}
}
How to compile java package?

javac -d directory javafilename

javac -d . Calculator.java
Accessing a package

import package.*;

import package.classname;
Demo.java
import letmecalculate.Calculator;
public class Demo{
public static void main(String args[]){
Calculator obj = new Calculator();
System.out.println(obj.add(100, 200));
}
}

You might also like