Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Packages in Java: Extra Lecture Overview Motivation Packages: Example for Packages. Some Definitions. packages. Packages: Extra Material 1 Motivation for Packages Until now, you have used classes from the Java API library by importing the package containing the class or classes you need. A package is a group of related classes. Packages are actually directory structures used to organise classes and interfaces Packages provide a mechanism for software reuse and a convention for unique class names . Packages make classes easier to find and use and are used to control access to classes. Packages potentially reduce compilation time because if a large program is broken down into several classes, only those classes that were modified after the last compilation will be recompiled. Packages: Extra Material 2 Steps for Creating Packages 1. Create as many directories as required. 2. Put related classes in the same directory. 3. Give the directory a meaningful name to reflect what the classes in the directory do. 4. Tell the Java system that each of these classes belongs to this package. You do this by adding a line like the following at the top of each class file: import myPackage; Note that step 2 is not sufficient for the java system to understand that the files in that directory belong to the same package. 5. Remember to declare public all classes in the package that are required to be used by other classes outside this package. 6. When your application is fully tested, debugged, and ready for deployment, use the JavaTM Archive file format to deploy the application. Packages: Extra Material 3 Experience with Packages: Example 1 Create a package, package1Lecture2, containing two classes A.java and D.java given below. package package1Lecture2; public class A { int a = 2; public int b = 3; protected int c = 5; private int d = 7; public A(){ System.out.println("Constructor for class A."); System.out.println("Value of a: "+ a); System.out.println("Value of b: "+ b); System.out.println("Value of c: "+ c); System.out.println("Value of d: "+ d); } } ------------------------------------------package package1Lecture2; class D{ public static void main(String args []){ A aObject = new A(); } } Packages: Extra Material 4 Experience with Packages: Example 1 (cont.) Compile, run and observe the output of the program. Make sure you understand every bit of the output, before developing this example further. Then, add the following class to package1Lecture2 and Add the line B bObject = new B(); in the main() method in class D shown in the preceding slide. package package1Lecture2; class B extends A { public B(){ System.out.println("Constructor for class B."); System.out.println("Value of a:"+ a); System.out.println("Value of b:"+ b); System.out.println("Value of c:"+ c); System.out.println("Value of d:"+ d); } } Packages: Extra Material 5 Experience with Packages: Example 1 (cont.) The package now fails to compile after adding this class. Use your knowledge of ICS102 to fix the error. Add the following class in the same fashion as you did for class B above. Remember to add the line C cObject = new C(); in the main() method in class D. package package1Lecture2; class C { public C(){ A obj = new B(); System.out.println("Constructor for class C."); System.out.println("Value of a:"+ obj.a); System.out.println("Value of b:"+ obj.b); System.out.println("Value of c:"+ obj.c); System.out.println("Value of d:"+ obj.d); }} Packages: Extra Material 6 Experience with Packages: Example 2 Create another package, package2Lecture2, using the following 3 classes. Notice that this new package uses classes in the package of Example 1. Use your knowledge of access modifiers to debug the program by commenting out some lines classes P, and Q. package package2Lecture2; class P extends package1Lecture2.A { public P(){ System.out.println("Constructor for class P in package2."); System.out.println("Value of a: "+ a); System.out.println("Value of b: "+ b); System.out.println("Value of c: "+ c); System.out.println("Value of d: "+ d); } } Packages: Extra Material 7 Experience with Packages: Example 2 package package2Lecture2; class Q { public Q(){ package1Lecture2.A aObject = new package1Lecture2.A(); System.out.println("Constructor for class C."); System.out.println("Value of a:"+ aObject.a); System.out.println("Value of b:"+ aObject.b); System.out.println("Value of c:"+ aObject.c); System.out.println("Value of d:"+ aObject.d); } } package package2Lecture2; class R{ public static void main(String args []){ P aObject = new P(); Q bObject = new Q(); } } Packages: Extra Material 8 Experience with Packages: Example 3 package package3Lecture2; public class P5{ static int twice(int x){ return 2*x; } public static int twiceSquare(int x){ return twice(x)*twice(x); } } ----------------------------------package package4Lecture2; public class P6 extends package3Lecture2.P5 { public static int twice(int x){ // super.twice(x); return twiceSquare(x); } } Packages: Extra Material 9 Experience with Packages: Example 3 (cont.) Test the given packages above using the following import package3Lecture2.P5; import package4Lecture2.P6; class TestP5P6 { public static void main(String [] args){ P6 p6Object = new P6(); System.out.println(p6Object.twice( 3)); } } -------------------------------- Can java packages be mutually recursive? That is, can a package P refer to classes in another package Q and vice-versa? Give an example to support your answer. Packages: Extra Material 10 Package Structure for Our Programs For the rest of this course you are required to organise all your programs into a package structure of the following form: cs Ai Architecture Automata Clientservers Concurrency Databases Datacommunication Datastructures Files Grammars Graphics Gui Html Images Lectures Logic Networking Operatingsystems Simulation Utilities Xml Packages: Extra Material 11