CONCEPT OF PACKAGES:
Packages is the collection of classes . Like in folder we handle files , hence for handling classes that belong to same category are kept in a single folder . This folder is called a PACKAGE . JAVA provides syntax for creating , as well ass accessing the classes of a package . We will see here how to create our own package and how to access a class for already existing package .
Packages remove the complexity . like folders provide organisation for files , in the similar way packages provide organisation of classes . packages make it easy to reach and search a file . Packages also provide the benefit of Data hiding .
INBUILT PACKAGES :
In-built packages are already available in java . A particular type of package contains classes with a similar property .
Understanding concept of packages is important as it is widely while preparing projects .
Some packages that are inbuilt are :
java.lang
java.util
java.id
java.net
java.sql
java.awd
java.awd.event
.
.
.
.
and many more .
Understanding how packages are written :
The format is :
a.b.c
Which means that package a contains another package b , and this statement would appera something like this
a.b
Now let b contains another package c .
that is .. a package contains another package b , b contains another package c
then if we want to refer package it is written like this :
a.b.c
Hence , java.awd.event means that we are referring to package event which is contained by package awd and further awd package is contained by package JAVA
Again note that packages are nothing but folder with collection of classes of similar category .
USER-DEFINED PACKAGES :
We need to create packages while developing projects to avoid confusion and make it easy for us to access classes .
CREATING A PACKAGE :
Let us say we want to create a package com which further contains alpha
And this package alpha we want to save our class for future use .
package com.alpha;
// Defines that class will belong to package alpha is contained by com package
public class Add
{
public void sum(int a,int b)
{
int c = a+b;
System.out.println("Sum "+c);
}
}
Note that Add class is declared as public here . To access a class of package it must be public . The methods of that class should also be public , only then they can be accessed out of package .
ANOTHER IMPORTANT POINT IN PACKAGES IS THAT ON A SAME NOTEPAD TWO PUBLIC CLASSES CANNOT BE CREATED FOR SINGLE PACKAGE.
TO ACCESS A PACKAGE :
- Only public classes of any package can be accessed.
- Import statement is used to access a class of a package
- In JAVA multiple import statements are possible. That is we can access classes from different packages as many as required
public class main
{
public static void main(String args[])
{
Add a=new Add();
a.sum(10,20);
}
}
* keyword implies that we can access all classes of package alpha which is contained by package com.
How to access multiple packages :
EXAMPLE :
Create a package with name pack1 and add class Add .
Create another package pack2 and add a Mul class to it which will perform multiplication method . pack2 should also contain another package pack3 and add class Div which will perform division operation .
And we have to access these three methods in same main class .
SOLUTION :
- Create a folder with name pack1.
- In notepad create class Add and save it in folder pack1.
- Note that it is important to run this class , only then it will be accessed by other programs
- Now for package, create another folder with name pack2 . Folder should not reside in any other folder .
- Now as per the demand of question , we need another package pack3 in pack2 with Div class . Make a folder in pack2 with name pack3 .
- Now we have created the three package . Next task is to call the call the method of these classes lying in different folders in a single main class.
0 comments:
Post a Comment