In the last post of modifier series, I have explained about the private access modifier , here I will explain about the default access modifier which is also called package level modifier.
Default Access modifier(package-level)
- Mostly,it is called as the package level modifier because using default let it conflict with the default key word used with switch statement.
- If we don't use any access modifier keyword then implicitly default access modifier is present there.
- To specify package level access modifier we don't need to use the default keyword explicitly.
- you can use package level access modifier with class also.
- A default member may be accessed only if the class accessing the members belongs to same package.
- To keep it in mind you can relate it like, you own a flat in a society , your own flat can resemble with private access as you can access only and your society can be called as the package as every one of society can access your flat externally.
Example :
here we will write a class in a package and we will try to access that from inside the package as well as from outside this package and we will see what our compiler complains in different scenarios.
first write a class with class members with default modifier
first write a class with class members with default modifier
now we will write two more classes to test our concepts:package defaultAccess; // a class in package with default access with members public class Parent { int parentVariableB; // no modifiers means default(implicitly) void parentMethodA() {
// no modifiers means default(implicitly)
System.out.println("do something"); } }
package defaultAccess;
// A subclass in same package and compiler is happy as well as jvm also
public class SubclassInSame extends Parent {
public static void main(String[] args) {
Parent pc = new Parent();
System.out.println(pc.parentVariableB); //no complain from compiler
pc.parentMethodA(); // compiler is happy
}
}
Now we will write a new class in same package but no relation with Parent:package defaultAccess;
// Another class in same package acceesing the default members of parent class
public class OtherClass {
public static void main(String[] args) {
Parent pc= new Parent();
System.out.println(pc.parentVariableB); // compiler is happy
pc.parentMethodA(); //here also compiler is happy
}
}
From above example you can conclude, at any point if we don't change the package till then compiler is happy and we can easily access the default members.Example 2:
Here we will create another class in different package and try to access default member present in different package(defaultAccess).
package defaultAccess2; // different package
import defaultAccess.*;
1. public class SubclassInDifferent extends Parent {
2. public static void main(String[] args) {
3. Parent ps = new Parent();
4. System.out.println(ps.parentVariableB); // compiler is not happy
5. ps.parentMethodA(); // compiler is not happy
}
}
At line no 4 and 5 , compiler complains
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The field Parent.parentVariableB is not visible
The method parentMethodA() from the type Parent is not visible
Now write a class in different package (defaultAccess2) and that class should not show any relationship with the Parent class.
package defaultAccess2;
import defaultAccess.*;
1. public class AnotherClass { // no relation with parent class
2. public static void main(String[] args) {
3. Parent ps = new Parent();
4. System.out.println(ps.parentVariableB); // compiler is not happy
5. ps.parentMethodA();// compiler is not happy
}
}
in above code also At line no 4 and 5 compiler complains as below :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The field Parent.parentVariableB is not visible
The method parentMethodA() from the type Parent is not visible
summary:
The package level accessifier restrict the accessibility and lets the members visible within package only. again you must be thinking what is use of making such restriction and how we can access the members then?
here it leads to the proper encapsulation concept where we make the method public and we will access the variables using the methods( JavaBean Style), i will explain in upcoming posts of OOPs series.
there is one more variation in package level modifier which adds some extra functionality and that called as protected access modifier that i will explain in next post.
No comments:
Post a Comment