Presentation is loading. Please wait.

Presentation is loading. Please wait.

Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations.

Similar presentations


Presentation on theme: "Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations."— Presentation transcript:

1

2 Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier etc. When a program is large, its classes can be organized hierarchically into packages. A Package can providing access protection and name space management. A collection of related classes and/or interfaces Programmers can define their own packages to bundle group of classes/interfaces

3 Using packages, it is easier to provide access control and it is also easier to locate the related classed. Package creates a new namespace there won't be any name conflicts with names in other packages

4 When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file. The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

5 To define: add package statement to specify package containing the classes of a file package mypackage; … public class myClass{ ----------------------------; } Must be first non-comment statement in file

6 Packages organized into subpackages using the notation foo.subpackage: package mypackage.mysubpackage; … public class myClass2 { … } // myClass2 is part of // mysubpackage, which is // within mypackage

7 T HE IMPORT K EYWORD : If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax. package payroll; public class Boss { public void payEmployee(Employee e) { e.mailCheck(); }

8 T HE IMPORT K EYWORD : The package can be imported using the import keyword and the wild card (*). import payroll.*; import payroll.Employee; Note: A class file can contain any number of import statements. The import statements must appear after the package statement and before the class declaration.

9 C LASS A CCESS AND P ACKAGES Class access within a package Classes within a package can refer to each other without full qualification If a class, or member within a class, is not declared public, it can only be accessed by other classes within the package Class access across packages A public class can be accessed from other packages Its name is fully qualified or it must be is imported to achieve this The public classes of a package can be seen as the interface of the package with the outside world Importing a package does not automatically import subpackages E.g. import java.awt.* does not import java.awt.font

10 T HE D IRECTORY S TRUCTURE OF P ACKAGES : The name of the package becomes a part of the name of the class, as we just discussed in the previous section. The name of the package must match the directory structure where the corresponding bytecode resides. // File Name : Car.java package vehicle; public class Car { // Class implementation. }


Download ppt "Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations."

Similar presentations


Ads by Google