Professional Documents
Culture Documents
Packages N Interfaces
Packages N Interfaces
Packages
To compartmentalize class names, stored in a hierarchical manner. E.g. io package is in java package. collection of classes. Naming and visibility control mechanism. Source file components: Package statement (optional) Import statements. (optional) Single public class declaration. (essential) Classes private to the package. (optional)
Package Use
Suppose directory gs exists and two people want to create two source files by their name. Problem is: their name is same say shobhit Solution is to create two folders under gs say p1 and p2 and ask each person to choose a directory p1 or p2. The fully qualified name for each program becomes p1.shobhit and p.shobhit. The directories p1 and p2 are synonymous to package p1 and p2 and the source files belong to either p1 or p2.
Example of package
package Gift;
class ScoobyDoo { public static void main(String args[]) { System.out.println("Java2 ab to aaja"); } }
E:\javaprogs\Gift>javac ScoobyDoo.java E:\javaprogs\Gift> E:\javaprogs\Gift>java ScoobyDoo Exception in thread "main" java.lang.NoClassDefFoundError: ScoobyDoo (wrong name : Gift/ScoobyDoo) E:\javaprogs>java Gift.ScoobyDoo Java2 ab to aaja E:\>java Gift.ScoobyDoo Exception in thread "main" java.lang.NoClassDefFoundError: Gift/ScoobyDoo
Access protection
Private
Same class Same package subclass
Yes No
No
Yes
Yes
Yes
No
No
Yes
Yes
No
No
No
Yes
Complete Example
Define 2 packages p1 and p2 inside E:\javaprogs CLASSPATH includes E:\javaprogs Create 4 source files in p1. Create 3 source files in p2 Use variables of p1 in p2.
public Protection() {
System.out.println("base constructor"); System.out.println("n = " + n); System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro); System.out.println("n_pub = " + n_pub); } }
package p1;
class Derived extends Protection { Derived() { System.out.println("derived constructor"); System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);
package p1;
class SamePackage {
SamePackage() { Protection p = new Protection(); System.out.println("same package constructor"); System.out.println("n = " + p.n); // class only // System.out.println("n_pri = " + p.n_pri); System.out.println("n_pro = " + p.n_pro);
package p1; // Instantiate the various classes in p1. public class Demo {
package p2;
// class only
// System.out.println("n_pri = " + p.n_pri); // class, subclass or package only // System.out.println("n_pro = " + p.n_pro); System.out.println("n_pub = " + p.n_pub); }}
package p2;
Execute Demo class in p2 like this: Step 1) e:\Javaprogs\p2> javac Demo.java Step 2) e:\Javaprogs\p2> cd .. Step 3) e:\Javaprogs>java p2.Demo The following out put appears: base constructor n=1 n_pri = 2 n_pro = 3 n_pub = 4 derived other package constructor n_pro = 3 n_pub = 4 base constructor n=1 n_pri = 2 n_pro = 3 n_pub = 4 other package constructor n_pub = 4
Importing packages
All inbuilt classes in Java are placed within some package. Package at the highest level is named java. To use a class without the fully qualified name, we use import stmt. Syntax: import p1.[p2.p3.]classname|*; * increases compilation time but not execution time. java.lang.*; automatically imported in program. If using import then it must the statement just after the package declaration. When importing packages only those items within package declared as public will be available to non-subclasses.
package p2; import p1.A; //import p1.*; class B extends A { public static void main(String args[]) { A obj = new A(); //System.out.println(obj.n1); //System.out.println(obj.n2); //System.out.println(obj.n3); System.out.println(obj.n4); B obj1 = new B(); //System.out.println(obj1.n1); System.out.println(obj1.n2); //System.out.println(obj1.n3); System.out.println(obj1.n4); } }
Interfaces
Syntactically similar to classes but lack instance variables and their methods are declared without a body. Once defined any number of classes can implement an interface. One class can implement several interfaces. To implement a class must implement all methods in an interface. This gives rise to form of multiple inheritance. They are preferred over abstract classes.
Syntax of Interface
Syntax: access interface name{ ret_type method1(paramlist); ret_type method2(paramlist); type fin_sta_varname1 = value; type fin_sta_vaname2 = value; } access = public or default. If interface public then all methods/variables inside are public.
Implementing an interface
Syntax: access class classname [extends Superclass] [implements interface i1, i2, i3] { //class Body }
About an Interface
Methods which implement an interface method must be declared as public. If a class implements 2 interfaces having same method name then same method will be used by clients of either interface. (obviously there is only one implementation of both the methods). If a class does not fully implement all method of an interface then it must be declared as abstract.
Example of interface
interface X { void method(); int x = 40; int c = 90; } interface Y { void method(); int x = 60; }
class Inter implements X, Y { public void method() { System.out.println("HI"); } public static void main(String args[]) { System.out.println(X.x); //40 System.out.println(Y.x); //60
System.out.println(c); //90
X obj1; Y obj2; Inter in = new Inter(); in.method(); //HI obj1 = in; obj1.method(); //HI obj2 = in; obj2.method(); // HI
} }
Extending interfaces
interface A { void f1(); }
interface B extends A { void f2(); }
class ExIn implements A,B { public void f1() { System.out.println("A"); } public void f2() { System.out.println("B"); } public static void main(String args[]) { ExIn obj = new ExIn(); obj.f1(); obj.f2(); } }