Package Concept Simplified In Java

0
18
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Package Concept in java
Package Concept in java

Package Concept Simplified In Java

Hide the implementation: Package concept

One of the main feature of Object Oriented Programming is its ability to reuse the code that is already created as part of other source file.Java provides an opportunity to separate the things that change from the  things that stay the same. For example a Library,the client who is using it should be able to rely on the part they are using and know that they wan’t have to rewrite code if a new version of library comes out.The library creator must have the freedom to make modification and improvements with utmost care so that the client programmer’s code won’t be affected by those changes.Sp this feature allows us to use the code without physically copying them into the program(under development).
The common conventions are:

  • The library programmer must agree not to remove existing methods when modifying a class in the library,as it may break the client code.
  • The library creator can not revamp the entire package or library.
Packages are ways to group varieties of classes or interfaces together.Packages can be considered as container for classes.To avoid the issue regarding this java provides access specifier to allow the library creator to specify something is available for consumers and remaining are not.Java does that by access specifiers.(public protected,private etc). This concept will ensure that all internal methods and data are private and only some method and data are exposed to the consumer.To understand it better lets us understand the .class file creation from a class. There can be only one public class in each compilation unit. When we compile a .java file,we get a .class file with same name as the public class. If there are many classes in a .java files, those many .class files will be created.These .class files are packaged together with linker to create an executable file.A working program is a bunch of .class files compressed in a jar.Java interpreter is responsible for finding ,loading and interpreting these files.There is nothing in java that forces the use of an interpreter. There exist native code , that produces a single executable file.A library is also a bunch of class files.Each file has one class that is public and there is one component for each file.

Packages are therefore classified into two types:

  • Java standard API packages(Example-java.lang,java.Util,java.io,java.awt,java.net etc)
  • User defined packages

Advantages of Package

  • Ability to reuse a code by sub classing existing classes (by inheritance).The classes contained in the packages of their program can be easily reused.
  • Collection of related classes(audio,video,text,graphics,project etc)
  • Make a structure that is compartmentalized.()
  • In a single package the classes are unique.But two classes of two packages can have same name.They should be refereed by their fully qualified name along with package and class name.
  • Package provides a way to hide classes thus preventing other programs or package from accessing classes that are meant for internal use only.
  • Packages also provide a way for separating “design” from “coding”.First we can design the classes and decide their relationship and then we can implement java code needed for the methods.It is also possible to change the implementation of any method without affecting the rest of the design. 

Import statement

When we want to use a predefined class in our code,the compiler must know how to locate it.In java we use import statement or keyword to bring in an entire library.The compiler will use this import statements to locate the actual class or where it is defined.

Case-I:

The class may exist in the same source code file that is being called from.Java eliminated the forward referencing problem,so we can use the class even if the class is not defined until later.

Case-II:

If the class exist in some other file.Java compiler has to feed the path of the specific class that we want to access.This is necessary as same class name may appear in different files.

import java.util.Vector
//Specify the full name only that particular class from the util package will be imported
import java.util.*;
//.* will wildcard the package name and includes all packages under util.

 

By using import keyword,we can tell compiler to bring in a package which is a library of classes.In the above example we are telling the compiler that we want to use java’s vector class from the utility package.Now if we want to use multiple classes of the util package, we can use the second example.

How package solve namespace or name collision issue

The reason for import statement is to provide a mechanism to manage ‘namespace’.Say classA has a foo() method and another class ClassB also has a foo() method. By the above way we can resolve the name collision and namespace.If there is any name collision like same class name is located under x and y both packages,in that case we have to use import x.nameoftheclass or y.nameoftheclass to use respective class.Java supports an unambiguous name for a library,the specifier used is like internet domain in reverse order as those arguments are unique in nature.

As my domain name is mydigitalvoice.in ,then the utility  library and ReadFile class will be IN.mydigitalvoice.Utility.ReadFile path where dot(.) stands for sub directories.In general COM is used instead of in. Capitalized COM or IN is by convention.

This mechanism means that all our files automatically live in their namespace and each class within a file automatically has an unique identifier(class names within a file must be unique).

How to create our own package


package MyPackage;//package declaration 
public class MyClass{//class definition 
public void test(){
System.out.println("Welcome to my class");
}
}
 

Once saved in MyClasss.Java,it will be located in a directory named MyPackage. After compilation,we will get MyClass.class file in the same directory.We are indicating the compiler unit that MyPackage library will also be part of the compilation unit.That is teh public class name within this compilation unit is under the umbrella of the name-MyPackage.If anyone wants to use the name,they will either have to fully specify the name or use the import statement in combination with MyPackage.If the name of the file is MyClass.java,this means there can be one and only one public class in that file. The name of that class must be in MyClass.

How to access System packages:

There are two ways to access the System packages:

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here