Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMP 110/401 P ACKAGES Instructor: Prasun Dewan.

Similar presentations


Presentation on theme: "C OMP 110/401 P ACKAGES Instructor: Prasun Dewan."— Presentation transcript:

1

2 C OMP 110/401 P ACKAGES Instructor: Prasun Dewan

3 2 P REREQUISITE Objects

4 3 A S IMPLE I NSTANTIATED C LASS public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } No (default) package

5 4 P ACKAGES package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.ASquareCalculator; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Class in different package must be imported using full name of class ( a la full file name)

6 5 P ACKAGES package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package lectures.functions; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Class in same package need not be imported

7 6 P ACKAGES package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } import lectures.functions.ASquareCalculator; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } No package means package named default, hence import needed

8 7 P ACKAGES public class ASquareCalculator { public int square( int x) { return x*x; } public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } No package means package named default, hence no import needed here

9 8 P ACKAGES public class ASquareCalculator { public int square( int x) { return x*x; } package main; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Short name of class in default package same as its full name

10 9 L ONG NAME WITH NO IMPORT package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; public class SquareCalculatorDriver { public static void main (String[] args) { lectures.functions.ASquareCalculator squareCalculator = new lectures.functions.ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Can use the full name of class directly

11 10 L ONG NAME WITH NO IMPORT package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.*; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Works but does not tell the reader what is being imported from the package name Important role of import is documentation Programming style violation

12 11 package lectures.safe_functions; public class ASquareCalculator { public long square( int x) { return x*x; } W HY IMPORTS / FULL NAME ? package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.ASquareCalculator; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Twice the size of ints Disambiguates

13 12 package lectures.safe_functions; public class ASquareCalculator { public long square( int x) { return x*x; } A MBIGUOUS I MPORT package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.ASquareCalculator; import lectures.safe_functions.ASquareCalculator; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Ambiguous

14 13 package lectures.safe_functions; public class ASquareCalculator { public long square( int x) { return x*x; } U NSUCCESSFUL I MPORT package lectures.functions; class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.ASquareCalculator; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); System.out.println (squareCalculator.square(5)); } Non public class usable only within its package

15 14 W HY P ACKAGES ? Can create competing implementations of same class. A la creating files Test.java in different assignment directories/folders Groups related classes together Can browse/search for related classes A la browsing through all files in an assignment directory/folder. Like directories/folders packages can be hierarchical package recitations.functions; package lectures.functions; Provides documentation of what unrelated classes are being used

16 15 B ROWSING J AVA C LASSES

17 16 B ROWSING J AVA C LASSES Very useful package

18 17 L ANGUAGE VS. L IBRARY Built-in classes Do not have to be explicitly imported

19 18 S TARTING O BJECT E DITOR I NTERACTIVELY java –classpath.;oeall20.jar Comp110ObjectEditor Can we tell short circuit this step

20 19 S TARTING O BJECT E DITOR P ROGRAMMATICALLY package lectures.functions; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import lectures.functions.ASquareCalculator; import bus.uigen.ObjectEditor; public class SquareCalculatorDriver { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); ObjectEditor.edit(squareCalculator ); } ObjectEditor is predefined packaged class

21 20 N O N EED TO E NTER C LASS N AME

22 21 R EMEMBERING P ACKAGE N AMES ? package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); ObjectEditor.edit(squareCalculator ); } What if we do not remember the package names? In Eclipse CTRL_SHIFT_O

23 22 A UTOMATIC I MPORTS IN E CLIPSE package math; public class ASquareCalculator { public int square( int x) { return x*x; } package main; import math.ASquareCalculator; import bus.uigen.ObjectEditor; public class SquareCalculatorTester { public static void main (String[] args) { ASquareCalculator squareCalculator = new ASquareCalculator(); ObjectEditor.edit(squareCalculator ); } Automatically added Gives a dialogue box in case multiple classes in same package Package names will not be given in class examples

24 23 E XTRA S LIDES

25 C OMP 401 P ACKAGES Instructor: Prasun Dewan

26 25 P REREQUISITES Objects

27 26 G ROUPING F ACTORIES Honda factories Car Factories Civic Factories Tablet Factories Corvette Factories Accord Factories Wuhan Factory Indiana Factory

28 27 G ROUPING C LASSES Package

29 28 G ROUPING C LASSES Package


Download ppt "C OMP 110/401 P ACKAGES Instructor: Prasun Dewan."

Similar presentations


Ads by Google