Presentation is loading. Please wait.

Presentation is loading. Please wait.

Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.

Similar presentations


Presentation on theme: "Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2."— Presentation transcript:

1 Packages

2 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2 Packages A package is a grouping of related types (i.e. classes and interfaces) Packages provide access protection and avoid naming conflicts Ex. Classes for reading and writing (input and output) are in the java.io package

3 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 3 Classes and interfaces can be packaged for the following reasons: Users and programmers can easily determine that these types are related. The names of the classes and interfaces won't conflict with the type names in other packages You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package (i.e. using protected variables)

4 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 4 Creating packages First, create a MathFunc class public class MathFunc { public static double PI = 3.14; static double OURPI = 3.1415926; private static double MYPI = 3.1416; public static double sqr( double x ) { return x*x; }

5 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 5 Then, create a Circle class public class Circle { double radius; public Circle( double r ) { radius = r; } public double getArea() { return MathFunc.OURPI*MathFunc.sqr( radius ); }

6 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 6 At the very first line of each class, put package ; Ex. On the MathFunc and Circle class, put package mypack; Then, recompile all the classes in the package

7 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 7 Usage To use the classes in the package, simply copy the folder containing the classes in the package as a subdirectory of your new class Rename the subdirectory with the same name as your package name After, put an import command on the first lines of you code as you would import other java classes (like java.io )

8 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 8 Usage example Make a new class named SampUse import mypack.*;//this line imports the mypack package //classes public class SampUse { public static void main( String args[] ) { System.out.println( MathFunc.sqr( 5 ) ); System.out.println( MathFunc.PI ); //System.out.println( MathFunc.MYPI ); //System.out.println( MathFunc.OURPI ); Circle c = new Circle( 10.0 ); System.out.println( c.getArea() ); }

9 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 9 Notes Make sure that the package name MATCHES the package directory Make sure that the package you import ALSO has the same name as the package name AND the package subdirectory Protected attributes/methods inside the package are now treated as private attributes/methods of the importing class.


Download ppt "Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2."

Similar presentations


Ads by Google