Any
package can be imported with keyword import.
To
import package name data_stucture
import
data_structure.*;
Above
statement imports all the classes, interfaces and other types in the
package, but it will not import the sub packages. So to import sub
package “linear” use the below statement.
Above
diagram shows the directory structure.
- Directory named “data_structure” contains two sub directories linear and non-linear.
- Directory linear contains two java files named Stack.java, DataStructure.java
package data_structure.linear; public interface DataStructure{ int MAX_SIZE = 1000; void display(); void insert(int data); void delete(); int search(int data); }
package data_structure.linear; public class Stack implements DataStructure{ int top = -1; int stack[] = new int[DataStructure.MAX_SIZE]; public void display(){ for(int i=0; i<=top; i++){ System.out.println(stack[i]); } } public void insert(int data){ if(top < DataStructure.MAX_SIZE-1){ top++; stack[top] = data; } else{ System.out.println("Stack is not empty"); } } public void delete(){ if(top == -1){ System.out.println("Stack is Empty"); } else{ stack[top] = 0; top--; } } public int search(int data){ for(int i=0; i < top; i++) if(stack[i] == data) return 0; return -1; } }
Lets
say StackTest.java is in the package data_structure.
package data_structure; import data_structure.linear.*; class StackTest{ public static void main(String args[]){ DataStructure myStack1 = new Stack(); myStack1.delete(); myStack1.insert(10); myStack1.insert(20); myStack1.display(); } }
Statements
to execute the StackTest.java
C:\>javac
-cp . data_structure\linear\DataStructure.java
C:\>javac
-cp . data_structure\linear\Stack.java
C:\>javac
-cp . data_structure\StackTest.java
C:\>java
-cp . data_structure.StackTest
Stack
is Empty
10
20
if
StackTest.java is in C drive and not in any specific package like
below
import data_structure.linear.*; class StackTest{ public static void main(String args[]){ DataStructure myStack1 = new Stack(); myStack1.delete(); myStack1.insert(10); myStack1.insert(20); myStack1.display(); } }
You
can compile and run as usual like below
import
can be used in 3 ways
1. Refer
to the member by its fully qualified name class StackTest{ public static void main(String args[]){ data_structure.linear.DataStructure myStack1 = new data_structure.linear.Stack(); myStack1.delete(); myStack1.insert(10); myStack1.insert(20); myStack1.display(); } }
Output
Stack is Empty 10 20
As
you observe the statement
data_structure.linear.DataStructure
myStack1 = new data_structure.linear.Stack();
The
class Stack and interface DataStructure are referred by full names.
And program not imported any package explicitly.
2. Import
the package member
import data_structure.linear.DataStructure; import data_structure.linear.Stack; class StackTest{ public static void main(String args[]){ DataStructure myStack1 = new Stack(); myStack1.delete(); myStack1.insert(10); myStack1.insert(20); myStack1.display(); } }
Output
Stack is Empty 10 20
As
you observe the statements
import
data_structure.linear.DataStructure;
import
data_structure.linear.Stack;
Program
import the package members DataStructure, Stack.
3. Import
the member's entire package
import data_structure.linear.*; class StackTest{ public static void main(String args[]){ DataStructure myStack1 = new Stack(); myStack1.delete(); myStack1.insert(10); myStack1.insert(20); myStack1.display(); } }
Output
Stack is Empty 10 20
Statement
“import data_structure.linear.*;” imports all the classes,
interfaces and other types in the package linear, but not the sub
packages.
Name
Ambiguities
If
a member in one package shares its name with a member in another
package and both packages are imported, you must refer to each member
by its qualified name. For example, the graphics package defined a
class named Rectangle. The java.awt package also contains a Rectangle
class. If both graphics and java.awt have been imported, the
following is ambiguous.
Rectangle
rect;
In
such a situation, you have to use the member's fully qualified name
to indicate exactly which Rectangle class you want. For example,
graphics.Rectangle
rect;
No comments:
Post a Comment