Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Packages and 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)

(a) (b) (c) (d)

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.

Syntax for declaring a package


Package declaration should be the first statement in the source file. Syntax: package somename; A source file belonging to some package must be stored under a directory with the name being the same as that of the package (Case Sensitive). Multilevel package declaration syntax: package p1.p2.p3; Here p3 is inside p2 and p2 is inside p1 and our source file is inside p3.

Finding packages and CLASSPATH


By default, JR system uses current working directory as its starting point for finding the package. Directory path/paths can be specified by using CLASSPATH environment varaible. Example: to execute program with package stmt. Package MyPack; program must be executed from a directory immediately above MyPack or CLASSPATH must include path to MyPack.

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

Setting CLASSPATH variable


Go to MyComputer properties Advanced Environment variables Select CLASSPATH Edit Add the required path For example: e:\javaprogs; If this done then last case in previous slide i.e. E:\>java Gift.ScoobyDoo executes successfully.

Access protection
Private
Same class Same package subclass

Default Yes Yes

Protected Yes Yes

Public Yes Yes

Yes No

Same package nonsubclass


Different package subclass Different package nonsubclass

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.

package p1; public class Protection { int n = 1;

private int n_pri = 2;


protected int n_pro = 3; public int n_pub = 4;

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);

System.out.println("n_pro = " + n_pro);


System.out.println("n_pub = " + n_pub); } }

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);

System.out.println("n_pub = " + p.n_pub);


} }

package p1; // Instantiate the various classes in p1. public class Demo {

public static void main(String args[]) {


Protection ob1 = new Protection();

Derived ob2 = new Derived();


SamePackage ob3 = new SamePackage(); } }

package p2;

class Protection2 extends p1.Protection {


Protection2() { System.out.println("derived other package constructor"); // class or package only // System.out.println("n = " + n); // class only // System.out.println("n_pri = " + n_pri); System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);


} }

package p2; class OtherPackage { OtherPackage() { p1.Protection p = new p1.Protection();

System.out.println("other package constructor");


// class or package only // System.out.println("n = " + p.n);

// 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;

// Instantiate the various classes in p2.

public class Demo {


public static void main(String args[]) { Protection2 ob1 = new Protection2(); OtherPackage ob2 = new OtherPackage(); } }

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

Some salient points


Unlike the instance variables classes have only two access specifiers: default and public. A class must be declared as public if imported/used outside its own package. There can only be a single public class declaration within a source file. (may/may not contain main) The constructor of a public class may/may not be declared as public.

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.

Another good example


package p1; public class A { private int n1 = 1; protected int n2 = 2; int n3 = 3; public int n4 = 4; }

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(); } }

There is always room at the top.


Chapter ends.

You might also like