The packages are used for categorization of the same type of classes and interface in a single unit. There is no core or in-built classes that belong to unnamed default package. To use any classes or interface in other class, we need to use it with their fully qualified type name. But some time, we’ve the need to use all or not all the classes or interface of a package then it’s a tedious job to use in such a way discussed. Java supports imports statement to bring entire package, or certain classes into visibility. It provides flexibility to the programmer to save a lot of time just by importing the classes in his/her program, instead of rewriting them.
In a Java source file, import statements occur immediately following the package statement (if it exists) and must be before of any class definitions. This is the general form is as follows:
import pkg1 (.pkg2). [classname|*l;
For example
import java.util.Vector;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
The star (*) increases the compilation time, if the package size is very large. A good programming methodology says that give the fully qualified type name explicitly, i.e. import only those classes or interface that you want to use instead of importing whole packages. Yet the start will not increase the overhead on run-time performance of code or size of classes.
Example:
//Student.java
package student;
class Student
{
private int rollno;
private String name;
private String address;
public Student(int rno, String sname, String sadd}
{
rollno = rno;
name = sname;
address = sadd;
}
public void show()
{
System.out.println(“Roll No :: ” + rollno);
System.out.println(“Name :: ” + name);
System.out.println(“Address :: ” + address);
}
}
// Test.java
package student;
class Test extends Student
{
protected int marksSubjecti;
protected int marksSubject2;
protected int marksSubject3;
protected int marksSubject4;
public Test(int rno, String sname, String sadd,int mi, int m2, int m3, int m4)
{
super(rno,sname,sadd);
marksSubjecti = mi;
marksSubject2 = m2;
marksSubject3 = m3;
marksSubject4 = m4;
}
public void show()
{
super.show();
System.out.println(“Marks of Subject1 :: ” + marksSubject1);
System.out.println(“Marks of Subject2 :: ” + marksSubject2);
System.out.println(“Marks of Subject3 :: ” + marksSubject3);
System.out.println(“Marks of Subject4 :: ” + marksSubject4);
}
}
//Result.java
package student;
public class Result ex..tends Test
{
private int totalMarks;
private float percentage;
private char grade;
public Result(int rno, String sname, String sadd,int mi, int m2, int m3, int m4)
{
super(rno,sname,sadd,ml,m2,m3,m4);
totalMarks = marksSubject1 + marksSubject2 + marksSubject3 + marksSubject4;
percentage = (totalMarks*100.00F/600.00F);
if (percentage >=50.00F)
grade=’D’;
else
if(percentage >=55.00F && percentage<=60.00F)
grade = ‘C’;
else
if (percentage >=6l.00F && percentage<=70.00F)
grade = ‘B’;
else
if(percentage >=7l.00F && percentage<=75.00F)
grade = ‘A’;
else
if (percentage >=76.00F && percentage<=85.00F)
grade = ‘H’;
else
grade = ‘S’;
}
public void show()
{
super.show();
System.out.println(“Total Marks :: ” + totalMarks);
System.out.println(“Percentage :: ” + percentage);
System.out.println(“Grade :: ” + grade);
}
}
//ImportPackageDemo.java
import student.Result;
public class ImportPackageDemo
{
public static void main(String ar[])
{
Result ob = new Result (1001, “Alice”, “New York”,135,130,132,138);
ob.show ();
}
}
In the source file ImportPackageDemo, import student. Result is the first statement; it will import the class Result from the student package.