Access modifiers in Java

Access modifiers are  important concept to build foundation in java

Access Modifiers:

Private
Protected
Public

Access levels in Java:

Private
Protected
Private
Default

Default is the access level you get if you don’t specify any access level

 

 

 At Class level

Access levels applicable at class level is
a)Public
b)Default

Class with public visibility can be accessed by all classes irrespective of the package.

Class with default visibility can be accessed by classes in the same package.
Classes outside the package cannot access them.

What does it mean for code in one class to access a member of another class in Java mean?

+When method code in one class can access a member in another class.
+When a subclass can inherit member of superclass.

 

A)Method code in one class can access a member in another class

class A {
public String printMethod()
{
return "This is class A method";
}
}

class B {
public void methodClass()
{
A aobj = new A();
aobj.printMethod();
}
}

 

Class B can invoke method of class A through (.) dot operator.

B)Subclass can inherit member of superclass

class X
 {
public String methodX()
{
return "This ia class x method";
}}
class Y extends X {public void methodY()
{
System.out.println("" +this.methodX()); 
//Directly access class X method
}}

 

At member level

a)public
b)default
c)protected
d)private

a)Public level of members variables in Java

When a method or a variable is declared public, all classes can access the members irrespective of the package

package AB;
public class AB {
public String methodAB()
{
return "This is class x method";
}
}package xy;
public class XY {
public static void main(String[] args)
{
AB abobj = new AB();
abobj.methodAB();}
}

 

In the above example even though class AB and XY are in different package, class XY can access methodAB() of class AB.

B)Private level of members variables in Java

When a method or a variable is declared private, only the code belonging to the class can access it.
No other class can access the class members.

package AB;
public class AB {
private String methodAB()
{
return "This is class x method";
}
}package xy;
public class XY {
public static void main(String[] args)
{
AB abobj = new AB();
abobj.methodAB();}
}

 

In the above example even though class AB and XY are in different package, class XY cannot access methodAB()
of class AB, it would throw compilation error

C)Default level of members variables in Java

When a method or a variable is declared default(nothing is mentioned), only the classes belonging to the same
package can access it.

package AB;
public class AB {
String methodAB()
{
return "This is class x method";
}
}package xy;
public class XY {
public static void main(String[] args)
{
AB abobj = new AB();
abobj.methodAB();}
}

 

Above example would throw error, since class AB and XY belong to different package.
To make it work,

package AB;
public class XY {
public static void main(String[] args)
{
AB abobj = new AB();
abobj.methodAB();}
}

Check: All Java Topics at one go

D)Protected level of members variables in Java

When a method or a variable is declared protected, only the classes belonging to the same
package can access it plus any subclasses outside the package.

package AB;
public class AB {
protected String methodAB()
{
return "This is class x method";
}
}package AB;
public class XY {
public static void main(String[] args)
{
AB abobj = new AB();
abobj.methodAB();}
}

 

In tha above example, class XY can access methodAB() of class AB since it is in the same package

package XY;
import Ab;
public class XY extends AB{
public static void main(String[] args)
{
this.methodAB(); //works
AB abobj = new AB();
abobj.methodAB(); // will not work}
}

 

In the above example, class XY can access methodAB() of class AB only through inheritance,trying to
access it through reference variable will throw error.

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>