In Modifier series, in Class level modifier comes first post i have explained why we should first take care about the access modifier attached to the class name?
Here I will explain different access modifier in details.
public
- As this word is self-explanatory , it says if you attach me with any class name or any class members like variables or methods then i will be accessible to every one in java world.
- you can relate it to the government property which say as it is government so any body can access it.
- For practical purpose we can consider the scenarios like if we don't use public then what problems we can face.
As for proper code convention we should adopt the practice of writing the class inside a package,follow my post here, so we write the code as below:
package packageFirst;
class FirstClass { // no public access-modifier used here
public int firstClassVariableA;
public void firstClassMethodA() {
}
}
Now consider a scenario where we will write another class in different package. And that class will try to extend the above written class that is FirstClass. so let us write the code as below :
package packageSecond;
import packageFirst.*;
public class Second extends FirstClass{ // here compiler will complain the
//type FirstClass is not visible.
}
here you can visualize if we write any class in one package without public access modifier and try to use that in different package then our java compiler complain regarding the visibility of class which is without public access modifier.To overcome this problem what we can do?
just a simple answer use public access modifier with class name and then that class will be visible every where in java world.
After using public access modifier , it requires only one thing, to use that class we need to use import that package or specific class in a package or a class, where we want to use that class.
so at class level access modifier the correct code will be as below, just use public keyword before the class name and member and then it can be used any where.
package packageFirst;
public class FirstClass { // used public modifier and problem is solved
public int firstClassVariableA;
public void firstClassMethodA() {
}
}
Example 2: here we will see what will happen, if we will not use public access modifier with class members and try to access those from different part of an application. let us write the code as below :
package packageFirst;
public class FirstClass {
int firstClassVariableA;// no public modifier used with variable
void firstClassMethodA() { // no public modifier used with method
}
}
now we will write a code and we will try to access the variable and method of FirstClass.
package packageSecond;
import packageFirst.*;
1. public class Second extends FirstClass {
2. public static void main(String[] args) {
3. FirstClass fc = new FirstClass(); // creating an object of FIRSTCLASSS
4. System.out.println(fc.firstClassVariableA); // compiler will complain
5. fc.firstClassMethodA(); // here also compiler will complain
}
}
from above code we can see line 1. is ok as we have used public with FirstClass class name so we can use it here after using import statement.At line no 3: we create an object of FirstClass. i will explain object creation in java in upcoming post.
At line no 4: here compiler will complain because we are trying to access the variable at different context where that variable is not visible. here compiler will ask you to change the visibility of firstClassVariableA to public or protected ( will explain in upcoming post)
At line no 5 : here also compiler will issue same complain as line no 4.
so to access the members of FirstClass and let the code works we need to make changes as below :
// changed code of example 2
package packageFirst; public class FirstClass { public int firstClassVariableA; // use public access modifier here public void firstClassMethodA() {
// use public access modifier here
} }
From above scenarios we can conclude that if you want any type(class,enum,interface,annotation) to be accessible anywhere in java universe then you have to use public access modifier and same rule is applicable for class members also(variables, methods,constructors).
if we don't use any modifier against any class name or any class members then implicitly it holds default access modifier(package -level modifier). In my next post i will explain about the default and private access-modifier.
No comments:
Post a Comment