0% found this document useful (0 votes)
51 views18 pages

15 Packages

The document discusses Java packages. It defines a package as a collection of classes, interfaces, and sub-packages. Packages are used to organize related classes and interfaces, share code between programs, and achieve code reusability. There are pre-defined packages developed by Oracle and user-defined packages developed by programmers. The document provides examples of creating packages, placing classes and interfaces within packages, compiling packages, and accessing classes within packages.

Uploaded by

stvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views18 pages

15 Packages

The document discusses Java packages. It defines a package as a collection of classes, interfaces, and sub-packages. Packages are used to organize related classes and interfaces, share code between programs, and achieve code reusability. There are pre-defined packages developed by Oracle and user-defined packages developed by programmers. The document provides examples of creating packages, placing classes and interfaces within packages, compiling packages, and accessing classes within packages.

Uploaded by

stvm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Packages

Java API
API - Application Programming Interface.
Java API - Collection of packages.

Package
collection of classes, interface and sub
packages in the form of .class file.

Purpose of package:
To place common classes and interfaces.
(share for other java programmers/Developers).
Without package concepts
• Programmers possible to develop own
code (Specific requirement).
• Not possible to get common requirement.
• Other programmer code not possible
access.

To solve above problem (Package introduced)


• Possible to access other user code using
package.
• Achieved common requirement.
JAVA PACKAGES
• Group of classes,interfaces, and sub packages.
• Two types of packages
– Build-in packages (Pre-defined packages).
– User defined packages.
• Allow features to reuse between program (with-
in) and across program (without).
• Way to reusable
class - class
interface - interface
class - interface
PRE-DEFINED PACKAGE
• Developed by sun micro system developer/oracle.
• Universal requirement.
Types of Pre-defined packages
1. Core (JSE) package.
2. Advance (JEE) package.
3. Mobile (JME) package.
List of Pre-defined core packages
1. java.lang.*; (default package in java).
2.java.awt.*; (abstract window tool kit)
3.java.awt.event.*;(action event)
4.java.io.*;(input and output stream)
5.java.applet.*;(applet browser)
6.java.net.*;(network)
7.java.util.*;(collection)
8.java.text.*;(text processing)
Sample program for pre-defined package
public class Packages_Sample
{
public static void main(String[] args)
{
java.util.Scanner s=new java.util.Scanner(System.in);
System.out.println("Enter your name");
String name=s.next();
System.out.println("Your name is "+name);
}
}

OUTPUT
Enter your name
Balaji
Your name is Balaji

here, pass value at run time.


Sample program for pre-defined package using import statement
import java.util.Scanner;
public class Import_Pack
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter first number");
int i1=s.nextInt();
System.out.println("Enter Second number");
int i2=s.nextInt();
int i3=i1+i2;
System.out.println("Sum of two number is "+i3);
}
}
OUTPUT
Enter first number
10
Enter second number
20
Sum of two number is 30
Example program for with out Scanner (To get value at run time)
class WithoutScanner
{
public static void main(String[] b)
{
String name=b[0];
String age=b[1];
System.out.println(name+" "+age);
}
}
Compile: javac WithoutScanner.java
Run: java WithoutScanner balaji 24
OUTPUT
balaji 24
User defined packages

• Developed by java programmer of other organisation.


• Common requirements.

Purpose
To place common classes and interfaces (user defined).

Syntax (To create user defined package)


package <package name>;
eg:
package demo;
here,
demo - package name (create folder after compiled)
STEPS AND GUIDELINES FOR PLACING CLASS AND INTERFACE
IN PACKAGE

1. Package statement must be first executable statement in


class and interface.
2. Access modifier must be public (appropriate class/
interface).
3. Access modifier for constructor must be public.
4. Access modifier for methods must be public for class
and optional for interface.
5. Save class name/interface name as file name.
6. Effective ( recommanded to pass class and interface in
seperate package) to avoid ambigulity problem.
Program sequence if using
package
Another syntax (parent package and sub package)
package <parent package name>.<sub package name>;

eg:
package test.paper;
package language.english.teach;

here,
test,language - parent package
paper,english - sub-package.
teach - sub-sub-package
How to compile package
Syntax:
javac -d<space>.<space><file name>.java
here,
javac - java compiler
-d - directory
. - Current directory
. . - parent directory
eg:
javac -d . Lists.java

How to Run package(main available in package)


Syntax:
java <package>.<class file>
eg:
java demo.Lists
Sample program for user defined package (with main method)

package detail;
public class List1
{
public static void main(String[] args)
{
System.out.println("Hello package");
}
}
compile package : javac -d . List1.java
Execute : java detail.List1

OUTPUT
Hello package
Sample program for user defined package (with out main method)
package menu; package finals;
public class Menu import menu.*;
{ import food.Food;
public int menuList(int add) public class Finals
{ {
return add; public static void main(String[] args)
} {
Menu m=new Menu();
}
int list=m.menuList(24);
save : Menu.java
Food f=new Food();
String name=f.foodDetail("POORI");
package food; System.out.println(name+" "+list);
public class Food }
{ }
public String foodDetail(String name)
{ save : Finals.java
return name; Compile
} javac -d . Menu.java
} javac -d . Food.java
save : Food.java javac -d . Finals.java
Execute java finals.Finals
Package example – commonly access by n number of
developer
package operation.add;
public class Addition
{
public int add(int[] adds)
{
int[] a=adds;
int sum=0;
int len=a.length;
for(int i=0;i<len;i++)
{
sum=sum+a[i];
}
return sum;
}
}

//save as :Addition.java
//compile as : javac -d . Addition.java
import operation.add.Addition;
import java.util.Scanner;
class Access
{
public static void main(String[] args)
{
Addition a=new Addition();
System.out.println("How many values to enter");
Scanner s=new Scanner(System.in);
int size=s.nextInt();
int[] data=new int[size];
System.out.println("start values to enter");
for(int i=0;i<size;i++)
{
data[i]=s.nextInt();
}
int sumOfArray=a.add(data);
System.out.println("sum of array is "+sumOfArray);
}
} //save as : Access.java
//compile as : javac Access.java
//execute as : java Access

You might also like