130 likes | 273 Views
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
E N D
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
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 }
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();
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
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 } }
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
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
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
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
Uses of Packages • Logical grouping of classes and interfaces • Avoiding clash of names • Provides an extra level of protection to its members
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