Java Packages Tutorial
Learn Packages in Java
Table of Contents
- What is a package in Java?
- Builtin Packages
- User defined packages
- Package Declaration in java program
- Why package?
- Access specifiers
What is a package in Java?
A Package contains a collection of classes, interfaces, and sub packages. These all are belongs to one group. Packages can be categorized into Two.
- Builtin Packages
- User defined packages
Builtin Packages
Ex:
java.lang.*;
java.io.*;
java.sql.*;
java.awt.*;
User defined packages
package mypack1;
package mypack2;
Package Declaration in java program
- first line package declaration
- importing statements
- class declaration.
syn:
package <packagename>;
ex:
package p1;
ex:
package p1; //package declaration
import java.io.*; //importing statements
class Test{ //class declaration. ........ ........ }
TOC
Why package?
To organizing similar group of classes into separate folder. So that, they can easily import by other java programs. It also to avoid naming conflicts.
Ex.
p1 can have class Class1.
p2 also can have class Class1.
So these classes referred with their package names as shown below…
p1.Class1;
p2.Class1;
ex2:
pakage A;
class Class1
{
display(){
System.out.println("This is from class1");
}
pakage B;
class Class2
{
display(){
System.out.println("This is from class2"); } class Test2 {
public static void main(String args[]){
Class1 ob1 = new Class1(); ob1.display(); Class2 ob2 = new Class2(); ob2.display();
}
GoTop
Access specifiers in Java
There are 4 types of Access Specifiers available in java to define the scope of the class members.
- private -- Allows to access with in the class
- default -- Allows to access with in the package (if we don't specify any specifier, then it is treated as default)
- protected -- Allows to access only by child. (the child can be from other package)
- public -- Allows accessing any class from same or outside the package. (no need to extend)
public class Accessvar
{
private int pri=5; int def=6;
protected int pro=7;
public int pub=8;
public Access()
{
System.out.println("private=" + pri);
System.out.println("default=" + def);
System.out.println("protected=" + pro);
System.out.println("public=" + pub);
}
}
UP↑
Another class in the samepackage(except private all are accessable)
class Test {
public static void main(String args[])
{
Accessvar av = new Accessvar();
System.out.println("private=" + av.pri);
System.out.println("default=" + av.def);
System.out.println("protected=" +av.pro);
System.out.println("public=" + av.pub);
}
}
Another class from diff package(protected, public)
class Test {
public static void main(String args[])
{
Accessvar av = new Accessvar();
System.out.println("private=" + av.pri);
System.out.println("default=" + av.def);
System.out.println("protected=" +av.pro);
System.out.println("public=" + av.pub);
}
}
Another child class from diff package(only protected, public)
class Test {
public static void main(String args[])
{
Accessvar av = new Accessvar();
System.out.println("private=" + av.pri);
System.out.println("default=" + av.def);
System.out.println("protected=" +av.pro);
System.out.println("public=" + av.pub);
}
}
Comments
Post a Comment