I will suggest you to read Access specifiers in JAVA before reading this post.
In java, code re-usability is an important feature. Code re-usability is nothing but reusing the same code that was already written.For example,a method for adding two numbers which will take two numbers as input is written in Calculation class.If Another class,Addition class needs to add two numbers, instead of writing the same code again,we can use the functionality that is defined in the Calculation class.
public class Calculation {
int add(int a,int b)
{
int c=a+b;
return c;
}
}
class Addition {
int a=15;
int c=35;
//We can use the add function defined in calculation class
}
How the Addition class will find the add function of Calculation class?
Creating the object of the Calculation class and add() function can be accessed.
This is a small example, in this only two classes are involved.Consider the scenario where huge application was developed using thousands of classes. In that case the classes needs to be organized under separate category. This can be achieved by means of packages. packages are bundle of classes and interfaces. Based on the functionality we can organize the classes and interfaces inside a package.
Let us consider Calculation class and Addition class are in same package,then add() method can be called by creating the object of calculation class.
//code to be used when calculation and Addition are in the same package.
Calculation cal=new Calculation();
int c=cal.add(15,20);
In case ,the addition class is in different package, then that package needs to be imported in the calculation class .Then only the calculation class will have access to classes and methods in Package where Addition class is defined.Consider Calculation class is in package1 and Addition class is in package2.
package package1;
public class Calculation {
public int add(int a,int b)
{
int c=a+b;
return c;
}
}
--------------------------------------------------------------------------------------------------------------------------
package package2;
import package1.*;
public class Addition {
int a=15;
int b=35;
public static void main(String args[]){
Calculation cal=new Calculation();
int c=cal.add(15,20);
System.out.println("the value of c is "+c);
}
}
--------------------------------------------------------------------------------------------------------------------------
There is an import statement present in the Addition class that will import all the public methods and variables in the calculation class.
Types of package:
In java, code re-usability is an important feature. Code re-usability is nothing but reusing the same code that was already written.For example,a method for adding two numbers which will take two numbers as input is written in Calculation class.If Another class,Addition class needs to add two numbers, instead of writing the same code again,we can use the functionality that is defined in the Calculation class.
public class Calculation {
int add(int a,int b)
{
int c=a+b;
return c;
}
}
class Addition {
int a=15;
int c=35;
//We can use the add function defined in calculation class
}
How the Addition class will find the add function of Calculation class?
Creating the object of the Calculation class and add() function can be accessed.
This is a small example, in this only two classes are involved.Consider the scenario where huge application was developed using thousands of classes. In that case the classes needs to be organized under separate category. This can be achieved by means of packages. packages are bundle of classes and interfaces. Based on the functionality we can organize the classes and interfaces inside a package.
Let us consider Calculation class and Addition class are in same package,then add() method can be called by creating the object of calculation class.
//code to be used when calculation and Addition are in the same package.
Calculation cal=new Calculation();
int c=cal.add(15,20);
In case ,the addition class is in different package, then that package needs to be imported in the calculation class .Then only the calculation class will have access to classes and methods in Package where Addition class is defined.Consider Calculation class is in package1 and Addition class is in package2.
package package1;
public class Calculation {
public int add(int a,int b)
{
int c=a+b;
return c;
}
}
--------------------------------------------------------------------------------------------------------------------------
package package2;
import package1.*;
public class Addition {
int a=15;
int b=35;
public static void main(String args[]){
Calculation cal=new Calculation();
int c=cal.add(15,20);
System.out.println("the value of c is "+c);
}
}
--------------------------------------------------------------------------------------------------------------------------
There is an import statement present in the Addition class that will import all the public methods and variables in the calculation class.
Types of package:
- Built in package
- User defined package
Sign up here with your email
ConversionConversion EmoticonEmoticon