Short note on packages in java and the access protection they provide
Answers
Answered by
2
u can search on google short on..................
butterflyqueen:
If you don't know then don't answer
Answered by
1
Packages are meant for encapsulating, it works as containers for classes and other subpackages. Class acts as containers for data and methods. There are four categories, provided by Java regarding the visibility of the class members between classes and packages:
Subclasses in the same package
Non-subclasses in the same package
Subclasses in different packages
Classes that are neither in the same package nor subclasses
The three main access modifiers private, public and protected provides a range of ways to access required by these categories.
ACCESS PROTECTION IN PACKAGES
Simply remember, private cannot be seen outside of its class, public can be access from anywhere, and protected can be accessible in subclass only in the hierarchy.
A class can have only two access modifier, one is default and another is public. If the class has default access then it can only be accessed within the same package by any other code. But if the class has public access then it can be access from any where by any other code.
Example:
//PCKG1_ClassOne.java
package pckg1;
public class PCKG1_ClassOne{
int a = 1;
private int pri_a = 2;
protected int pro_a = 3;
public int pub_a = 4;
public PCKG1_ClassOne() {
System.out.println("base class constructor called");
System.out.println("a = " + a);
System.out.println("pri_a = " + pri_a);
System.out.println("pro_a "+ pro_a);
System.out.println("pub_a "+ pub_a);
}
}
The above file PCKG1_ClassOne belongs to package pckg1, and contains data members with all access modifiers.
//PCKG1_ClassTwo.java
package pckg1;
class PCKG1_ClassTwo extends PCKG1_ClassOne {
PCKG1_ClassTwo() {
System.out.println("derived class constructor
called");
System.out.println("a = " + a);
II accessible in same class only
II System.out.println("pri_a = " + pri_a);
System.out.println("pro_a "+ pro_a);
System.out.println("pub_a =” + pub_a);
}
}
The above file PCKG1_ClassTwo belongs to package pckg1, and extends PCKG1_ClassOne, which belongs to the same .
Subclasses in the same package
Non-subclasses in the same package
Subclasses in different packages
Classes that are neither in the same package nor subclasses
The three main access modifiers private, public and protected provides a range of ways to access required by these categories.
ACCESS PROTECTION IN PACKAGES
Simply remember, private cannot be seen outside of its class, public can be access from anywhere, and protected can be accessible in subclass only in the hierarchy.
A class can have only two access modifier, one is default and another is public. If the class has default access then it can only be accessed within the same package by any other code. But if the class has public access then it can be access from any where by any other code.
Example:
//PCKG1_ClassOne.java
package pckg1;
public class PCKG1_ClassOne{
int a = 1;
private int pri_a = 2;
protected int pro_a = 3;
public int pub_a = 4;
public PCKG1_ClassOne() {
System.out.println("base class constructor called");
System.out.println("a = " + a);
System.out.println("pri_a = " + pri_a);
System.out.println("pro_a "+ pro_a);
System.out.println("pub_a "+ pub_a);
}
}
The above file PCKG1_ClassOne belongs to package pckg1, and contains data members with all access modifiers.
//PCKG1_ClassTwo.java
package pckg1;
class PCKG1_ClassTwo extends PCKG1_ClassOne {
PCKG1_ClassTwo() {
System.out.println("derived class constructor
called");
System.out.println("a = " + a);
II accessible in same class only
II System.out.println("pri_a = " + pri_a);
System.out.println("pro_a "+ pro_a);
System.out.println("pub_a =” + pub_a);
}
}
The above file PCKG1_ClassTwo belongs to package pckg1, and extends PCKG1_ClassOne, which belongs to the same .
Similar questions