1 / 12

Packages

Packages. Packages in Java. A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces All the classes and interfaces that are used for performing I/O operations can be placed in the same package

nelson
Télécharger la présentation

Packages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Packages

  2. Packages in Java • A package is a collection of related classes and interfaces in Java • Packages help in logical grouping of classes and interfaces • All the classes and interfaces that are used for performing I/O operations can be placed in the same package • All the classes and interfaces that are used for writing network programs can be placed in the same package

  3. Creating a Package • It would be better to group all the classes and interfaces related to insurance in a package, insurance • Create a folder insurance • Place the following program in this folder package insurance; public class Policy{ //Code goes here }

  4. Creating a Package • Compile the program • class Policy will be created in a package insurance • The package statement, if existing, should be the very first statement of the file • The fully qualified name of a class that is stored in a package is <packagename>.<ClassName> • insurance.Policy insurance.Policy policy = new insurance.Policy();

  5. Packages to Prevent Name Clash • Packages prevent the clash of class/interface names • More than one class/interface can have the same name • They should be in two different packages • The fully qualified names of these classes/interfaces will be different

  6. Importing a Class • It is difficult to use the fully qualified name of a class through out the program • The import statement can be used to import a class into a program so that the class name need not be fully qualified import insurance.Policy; class Test{ public static void main(String [] args){ Policy policy = new Policy(); //Policy means insurance.Policy } }

  7. Importing all Classes and Interfaces • The statement import insurance.* • Imports all the classes and interfaces in the package insurance • All the classes and interfaces in this package can be used without qualifying them with package name

  8. Packages and classpath • Compiler and JVM must know location of the packages • An environment variable classpath needs to be set • The classpath will be pointing to a folder be one level up the package folder • If the folder hierarchy is C:\work\java\insurance, the classpath should be set using the following statement >set classpath = %classpath%;C:\work\java

  9. Sub Packages • A package can in turn contain another sub package • To create a sub package policy in insurance • Create a sub folder policy inside the folder insurance • Place the following code in the folder policy and compile package insurance.policy; public class Policy{ //Code goes here } • A class Policy will be created in a package insurance.policy • Fully qualified name will be insurance.policy.Policy

  10. Access Modifiers • Java has 4 access control modifiers • private: Accessible only within the class • default: No keyword, Accessible only within the package • protected: Similar to default with the addition that available to all child classes; that is, even if child class is in a different package • public: Accessible to all • Data Members and Methods can have any of these specifier • Classes and Interfaces can have either the public access or the default access

  11. Uses of Packages • Logical grouping of classes and interfaces • Avoiding clash of names • Provides an extra level of protection to its members

  12. Standard Java Packages • java.lang • Contains classes that form the basis of the design of the Java programming language • No need to explicitly import this package • The String class, System class etc, belong to this package • java.io • Classes and interfaces to perform I/O operations • java.util • Utility classes and interfaces like List, Calendar etc • java.awt • Classes and interfaces to create GUI

More Related