Vous êtes sur la page 1sur 7

Packages in Java

Package in Java is a mechanism to encapsulate a group of


classes, sub packages and interfaces. [A package is a
mechanism to group the similar type of classes, interfaces
and sub-packages and provide access control. ] It
organizes classes into single unit. Packages are used for:

 Preventing naming conflicts. i.e. there can be two


classes with name Employee in two different packages
 Making searching/locating and usage of classes,
interfaces, enumerations and annotations easier
 Providing controlled access: protected and default have
package level access control. A protected member is
accessible by classes in the same package and its
subclasses. A default member (without any access
specifier) is accessible by classes in the same package
only.
 Packages can be considered as data encapsulation (or
data-hiding).

Types of packages:

 Built-in Packages
These packages consist of a large number of classes
which are a part of Java API. Some of the commonly used
built-in packages are:
1) java.lang: Contains language support classes(e.g
classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output
operations.
3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for
Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the
components for graphical user interfaces (like button ,
;menus etc).
6) java.net: Contain classes for supporting networking
operations.

[ javax.swing, java.text, java.sql........]

 User Defined Packages: While creating User Defined


Packages Java, the order of statements is very important.
The order must be like this, else, compilation error will
result.

1. Package statement
2. Import statement
3. Class declaration

After writing the code we must compile with package notation.


Package notation uses –d compiler option as follows.

C:\> javac -d . programname.java

The –d compiler option creates a new folder with the package


name and places the XYZ.class in it. The dot (.) is an operating
system's environment variable that indicates the current
directory. It is an instruction to the OS to create a directory
called forest and place the XYZ.class in it.

Sample programs

1.Demonstrate user defined package.

Type the following in a notepad named XYZ.java


package cme1;
import java.util.*;
public class XYZ
{
public void getDetails(String cName, int code)
{
System.out.println("Class name is " + cName);
System.out.println("Class code is " + code);
}
}
Type the following code in a notepad named
packageDemo.java
import cme1.XYZ;
public class PackageDemo
{
public static void main(String args[])
{
XYZ t1 = new XYZ ();
t1.getDetails("3 CME", 50);
}
}

The compilation and execution is as follows.

D:\> javac -d . XYZ.java

D:\> javac PackageDemo.java


D:\> java PackageDemo

2.Write a program to create a user defined package in


Java.
Type the following code and save it as Vehicle.java

package vehicles;
interface Vehicle
{
public void run();
public void speed();
}

Car.java
package vehicles;
public class Car implements Vehicle
{
public void run()
{
System.out.println("Car is running.");
}
public void speed()
{
System.out.println("Speed of Car: 50 Km/h");
}
public static void main(String args[])
{
Car Car = new Car();
Car.run();
Car.speed();
System.out.println("Hello World!");
}
}

Output:
3. Write a Package MCA which has one class Student. Accept
student detail through parameterized constructor. Write display
() method to display details. Create a main class which will use
package and calculate total marks and percentage.

Notepad----------Student.java
/*In this example we create a package mca which is used to
display the student records. This package is used by
Studentmain class. */
package mca;
public class Student
{
public int r_no;
public String name;
public int a,b,c;
int sum=0;
public Student(int roll, String nm, int m1,int m2,int m3)
{
r_no = roll;
name = nm;
a = m1;
b = m2;
c = m3;
sum = a+b+c;
}
public void display()
{
System.out.println("Roll_no : "+r_no);
System.out.println("Name : "+name);
System.out.println("-----MARKS-------");
System.out.println("Sub 1 : "+a);
System.out.println("Sub 2 : "+b);
System.out.println("Sub 3 : "+c);
System.out.println("Total : "+sum);
System.out.println("percentage: "+sum/3);
System.out.println("------------------");
}
}

note pad ----StudentMain.java


import mca.Student;
import java.util.*;
import java.lang.*;
import java.io.*;
class StudentMain
{
public static void main(String[] args)
{
String nm;
int roll;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll no:= ");
roll = sc.nextInt();
System.out.print("Enter Name:= ");
nm = sc.next();
int m1,m2,m3;
System.out.print("Enter 3 sub mark:= ");
m1 = sc.nextInt();
m2 = sc.nextInt();
m3 = sc.nextInt();
Student s = new Student( roll,nm,m1,m2,m3);
s.display();
}
}

Output:

Vous aimerez peut-être aussi