In this Part, we will check how the specifiers behave when the classes defined in
different packages are trying to access member variables of other classes.
Private Access Specifier
Consider the following code,
package com.vk;
public class Vk {
private int a=40; // Access Specifier -> Private.
}
package com.vk.first;
import com.vk.Vk;
class Vk1
{
public static void main(String args[])
{
System.out.println("Creating a new Object");
Vk v=new Vk();
System.out.println("Trying to access the Private member variable");
v.a=50;
}
}
In the above program, we have a defined a member variable 'a' with
Access Specifier -> Private and we are trying to access this variable from a class
'Vk1'which is defined in a different package.
Run this program and check if the compilation is success.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Vk.a is not visible
at com.vk.first.Vk1.main(Vk1.java:14)
Private Variables cannot be accessed by any other class, they can only
be accessed in the
class where they are defined.
Protected Access Specifier
Now lets change the access specifier for our variable 'a' to protected and check if
compilation is clean.
package com.vk;
public class Vk {
protected int a=40; // Access Specifier -> Protected.
}
package com.vk.first;
import com.vk.Vk;
class Vk1
{
public static void main(String args[])
{
System.out.println("Creating a new Object");
Vk v=new Vk();
System.out.println("Trying to access the Protected member variable");
v.a=50;
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Vk cannot be resolved to a type
Vk cannot be resolved to a type
at com.vk.first.Vk1.main(Vk1.java:8)
Protected variables cannot be accessed from classes defined in different package.
Default Access Specifier
To define a variable with default access specifier, dont mention anything before
the data_type.
Now lets change the access specifier for our variable 'a' to default and check if
compilation is clean.
package com.vk;
public class Vk {
int a=40; // Access Specifier -> Default.
}
package com.vk.first;
import com.vk.Vk;
class Vk1
{
public static void main(String args[])
{
System.out.println("Creating a new Object");
Vk v=new Vk();
System.out.println("Trying to access the Default member variable");
v.a=50;
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Vk.a is not visible
at com.vk.first.Vk1.main(Vk1.java:14)
Default variables can be accessed by any other class defined in the same package.
Public Access Specifier
Now lets change the access specifier for our variable 'a' to Public and check if
compilation is clean.
package com.vk;
public class Vk {
public int a=40; // Access Specifier -> Public.
}
package com.vk.first;
import com.vk.Vk;
class Vk1
{
public static void main(String args[])
{
System.out.println("Creating a new Object");
Vk v=new Vk();
System.out.println("Trying to access the Public member variable");
v.a=50;
}
}
Creating a new Object
Trying to access the Public member variable
No comments:
Post a Comment