Access modifiers
Access modifiers in java:-
Access modifiers decide that whether the class
,constructor,member
or method will be accessible or not.simply access modifier define the accessibility.
There are four types of modifier in java
- Access modifier ( private, default, protected and public )
- Non-access modifier. ( static, abstract, synchronized, native, volatile, transient etc.)
Now we will discuses access modifier only.
for your understanding I divide access modifier into to type
a. class level modifier <class level >
b. member level modifier.<member level>
a.class level modifier
-In class level Public and default modifiers are used.
-In class only public modifier are written by-default default are applied.
Public access modifier
if we use public key word with class it will be accessible in every where.It has highly scope among all modifier.
example
//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
output
I'm class1 method
default access modifier
-if we don't use public default are applied by-default.
-it is also called package-private because if the class has default keyword it has private for other package.
//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
output
b. member level modifier.<member level>
public access modifier
-If public keyword are use method will be calledwill be called from anywhere.
can access in same class.
can access in inherit class.
can access other package.
example
//package 1
package accessModifiers;
public class Class1 {
public void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
output
I'm class1 method
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
default access modifier
-if we don't use any modifier default are used.
default access modifier method and variable are access only in same class.
-if we don't use any modifier default are used.
default access modifier method and variable are access only in same class.
protected access modifier
-protected access modifier are use the method.
- can access in same package.
- can access in other package in inheritance only.
package accessModifiers;
public class Class1 {
protected void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
output
error
error
private protected access modifier
if private protected access modifier isuse with method or member variabel they will be only access in inheritance classes no issue if they are of same package or different.
private access modifier
private provide highest level protection.
-it can access only in it own class.
//package1
package accessModifiers;
public class Class1 {
private void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
package accessModifiers;
public class Class1 {
private void display()
{
System.out.println("I'm class1 method");
}
}
//package2
package accessModifiers2;
import accessModifiers.*;
class Class2 extends Class1 {
public static void main(String args[])
{
Class1 c=new Class1();
c.display();
}
}
Comments
Post a Comment