Package
A Java package is a mechanism for organizing Java classes into a single directory (or package). Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
If any method or variable modified as a public it is accessible from any where in java universe.
If there is no modifier it is default modifier.
class can only use public and default modifiers. Any other access modifiers can not use for a class.
example (private modifier)
class PP{
void m(){
System.out.println("default modifier");
}
}
class P1 extends PP{
public static void main(String args[]){
P1 q = new P1();
q.m();
}
}
But if we modified m() method as private see what happens
class PP{
private void m(){
System.out.println("private modifier");
}
}
class P1 extends PP{
public static void main(String args[]){
P1 q = new P1();
q.m();
}
}
It gives you compile error. That mean private methods can only access within the class. Even though P1 is a sub class of PP its not possible.
Try yourself to demonstrate other modifiers like above example. Then you'll never forget access modifiers.
-Thank you-





No comments:
Post a Comment