Header Ads

Member Modifiers in java | Access Modifiers-in-java-Pingjava

Member Modifiers in java

Member Modifiers in java-Pingjava





Member modifiers:


Public members:


If a member declared as the public then this member can be access from anywhere "but the

respective class should be visible" So before checking member visibility we need to

check class visibility.



Example:


Program 1:


package packl;

class A

{

public void methodOne()

{

 System.out.println("a class method");

}

 





Program 2:


package pack2;

import pack1.A;

class B

{

public static void main(String args[])

{

A a=new A();

a.methodOne();

}

}



Output:

       javac -d. A.java    -------->(run on cmd)

       javac -d. B.java    -------->(run on cmd)

       Compile time error  -------->(output on cmd)




In the above Program even though methodOne() method is public we can't access from class B because the corresponding class A is not public that is both classes and methods are public

then only we can access.






Default member:

If a member declared as the default then we can access that member only within the current package hence default member is also known as package level access.



Example 1:

Program 1:

package pack 1;

class A

{

void methodOne()

{

 System.out.println("methodOne is executed");

}

}





Program 2:


package pack1;

import packl.A:

ciass B

{

public static void main(String args[])

{

A a=new A();

a.methodOne();

}

}



Output:

       javac -d. A.java       ----->(run on cmd)

       javac -d. B.java       ----->(run on cmd)

       java pack1.B           ----->(run on cmd)

       methodOne is executed  ----->(output on cmd)



Example 2:


Program 1:


package pack1;

class A

{

void methodOne()

{

 System.out.println("methodOne is executed"):

}

}



Program 2:


package pack2;

import packl.A;

class B

{

public static void main(String args[])

{

A a=new A();

a.methodOne();

}

}



Output:

       javac -d. A.java        ----->(run on cmd)

       javac -d. B.java        ----->(run on cmd)

       Compile time error      ----->(output on cmd)











Private members:


1. If a member declared as the private then this member can be access only with in the

current class.


2. One thing should be remember that Private methods are not visible in child classes where as abstract methods should be visible in child classes to provide implementation hence private, abstract combination is illegal for methods.



Protected members:

1. If a member is prefixed with protected then we can access that member within the current package anywhere but outside package only in child classes.


2. Also Remember,We can access protected members within the current package anywhere either by child reference or by parent reference

3. But from outside package we can access protected members only in child classes and should be by child reference only that is we can't use parent reference to call protected members from outside package.




Example:

Program 1:


package packi:

public class A

{

protected void methodOne()

{

 System.out.printIn("mcthodOne is executed");

}

}






Program 2:


package pack1;

class B extends A

{

public static void main(String args[])

{

A a=new A();

a.methodOne();

B b=new B();

b.methodOne();

A al=new B();

al.methodOne();

}

}

Output: 

       javac -d. A.java          ----->(run on cmd)

       javac -d. B.java          ----->(run on cmd)

       java pack1.B              ----->(run on cmd)

       methodOne is exccuted     ----->(output on cmd)

       methodOne is executed     ----->(output on cmd)

       methodOne is executed     ----->(output on cmd)



Example 2:


package pack2;

import pack1.A;

public class C extends A

{

public static void main(String args[])

{

A a=new A();

a.methodOne();

C c=new C();

c.methodOne();

A al=new B();

a1.methodOne();

}

}


Output:

       javac -d. C.java   ---------->(run on cmd) 

       compile time error ---------->(output on cmd)



Compression of private, default, protected and public:

visibility

private default protected

public

1)With in the same class

2)From child class of same package X VN/

3)From non-child class of same

package

but we

4)From child class of outside

package

should use

child reference

only

5)From non-child class of outside

package



The least accessible modifier is private.

The most accessible modifier is public.


Private < default < protected < public



Recommended modifier for variables is private where as recommended modifier

for methods is public.


An Inspirational quote: 

"
Opportunity is missed by most people because it is dressed in overalls and looks like work." 



Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)

Another way to subscribe: Click here to Subscribe PingjavaOfficialSite by Email

Happy Reading.Happy Learning!!! See you in next post  
:)

No comments

Note: Only a member of this blog may post a comment.