Java- Example of packages

 //Example of package

package pack1;

public class protection
{
                  int n=1; //by default is default or friendly
private       int n_pri=2;
protected  int n_pro= 3;
public         int n_pub=4;

public protection()        // constructor
{
System.out.println(n);
System.out.println(n_pri);
System.out.println(n_pro);
System.out.println(n_pub);
}
}

package pack1;
public class derived extends protection {
 public  derived()
{

System.out.println(n);
System.out.println(n_pri);
System.out.println(n_pro);
System.out.println(n_pub);



}
}




package pack1;


public class nonsubclass  {

public nonsubclass()
{
pack1.protection obj=new pack1.protection();
System.out.println(obj.n);
//Systm.out.println(obj.priv);
System.out.println(obj.n_pro);
System.out.println(obj.n_pub);

}
}

//package pack 2

package pack2;

public class protection2 extends pack1.protection{

protection2()
{
System.out.println(n);
System.out.println(n_pri);
System.out.println(n_pro);
System.out.println(n_pub);
}

package pack2;

public class p2nonsubclass
{

pack1.protection obj1=new pack1.protection();
System.out.println(obj1.);
//Systm.out.println(obj1.);
//System.out.println(obj.n_pro);
//System.out.println(obj.n_pub);
}
}
// main file
public class Main {
public static void main(String[] args) {
//  pack1.protection obj=new pack1.protection();
    // pack1.derived obj =new pack1.derived();
pack1.nonsubclass obj=new pack1.nonsubclass();
  

}

Reactions: