• Package in java

    PACKAGE 

    Java provides a mechanism for partitioning the class name space into more manageable chance . This mechanism is called package . There may be situations two programmers wants to have same classname but this is not allowed . Since it will lead to name collision so there has to be mechanism to assure that name chosen for a class is reasonably unique and not collide with classnames chosen by other programmers . Package provides this mechanism. It keeps the classname space compartmentalized.

    •  Defining a Package

    To create a package , we simply include a package command as the first statement in a Java source file. This is the general form of the package statement:
    package pkg;
    Any ____ class file declared as a part of pkg must be stored in a directory called pkg.

    •  Importing Packages

    Java provides import statement to bring certain classes or entire package into visibility . Once imported , a class can be referred to directly using only its name.

    •  This is the general form of the import statement:

    import pkg1[.pkg2].(classname|*);

    Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, we specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package.
    Other than naming , package also provides a visibility control mechanism. We can ____ inside a package that are not accessible by code Outside the package . It si also possible to define class members that are exposed only to other members of the same package . Pacakage are also mean of encapsulating and containing the name space and the scope of variables and methods .


  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!