Package in java is a group of similar type of classes, interface and sub-package. Packages in java can be defined in two ways which are
1 Built-in Packages
2 User-defined Packages
Built-in Packages are
* Lang
* Util
* io
* awt
* net
* applet, etc.
User-defined packages can be anything which are created by the user.
Packages are nothing but container of the classes which store the no of classes, interface and their methods.
Advantages of Packages
1 It provide access Protection
2 It removes the naming collision
3 it is used to categorize the classes and interface so that they can be easily maintained.
Steps for creating a package
Step-1 Create a folder in any drive of your choice
Step-2 example in d: package1 (it is the name of the package)
Step-3 Open the java application and type the following code
package package1;
public class demo
{
public void display()
{
System.out.println("Welcome to Programmingssidepoint");
}
}
Step-4 Save the above code in the drive where you have saved the package as demo.java
Step-5 Now open a new file and type the following code
import package1.demo;
class example
{
public static void main(String[] args)
{
demo obj=new demo();
obj.display();
}
}
Step-6 Save the above code in the same drive in which you have created the folder that is package1
Step-7 example d:example.java
Step-8 Now compile and execute the following code.
Nice
ReplyDeleteVery helpful info. Thanks
ReplyDeleteThank sir for this info .
ReplyDelete