Page 453 - Cs_withBlue_J_C11_Flipbook
P. 453

15


                                                                                 PACKAGES
















                          Learning Objectives



                     15.1  Packages                                      15.2  Creation of User-Defined Packages in BlueJ
                     15.3  Importing User-Defined Packages


                 In a computer, folders are used to keep the files organised and separate. Imagine that all your study materials (docs or
                 pdfs) for  online classes sent by your subject teachers are kept in the same folder ‘Documents’. Your computer teacher
                 has given home-work on Number System. When you search for the required pdf for the reference, you find that there are
                 nearly 500 pdfs in your folder. Finding your file in the midst of the chaos became a herculean task. You then decided to
                 work smartly, making separate folders for your subjects and moving the files to the relevant directories. Folders also allow
                 you to have more than one file with the same file name. You may have the topic Boolean Algebra both in Computer and
                 Mathematics but their content may vary. So now you have two different files with the same name, Boolean Algebra, in
                 two directories, Computer and Mathematics. Java software is primarily a collection of classes. A package is a pack (group)
                 of classes, interfaces and other packages which are used to organise our classes and interfaces.


                     15.1 PACKAGES
                 Packages are a collection of similar types of classes and sub-packages.

                                                               Definition

                      PACKAGES in Java are the containers for groups of related classes, sub-classes, sub-packages, and interfaces. They
                      help to organise Java classes into a separate namespace, which makes them easy to locate.



                                                               Definition

                      Namespaces simply means scope. A namespace is created to group together those names that might be repeated
                      elsewhere within the same or interlinked programs, objects and elements.


                 15.1.1 Advantages of Packages
                 The advantages of using a package are listed below.
                 •  Reusability: There may be specific codes in Java that need to be used repeatedly. They can be written as methods
                   in various classes under a package. Importing the package in our program will serve the purpose.



                                                                                                                       451
                                                                                                            Packages   451
   448   449   450   451   452   453   454   455   456   457   458