Milind Chaudhary wrote:Hi.. I am getting an error in the following code
CODE:-
As I am using protected within the same package.. the program should compile.. but I am getting an error:
B.java:19: cannot find symbol
symbol : variable x
location: class B
a.getInt(x);
^
1 error
Please help!!
the only way you can access the protected variable is through inheritance................the above vaiable cannot be visible...........
class A
{
protected int x=9;
int y;
void getInt(int j)
{
y=j;
System.out.print("y=" + y);
}
}
public class B extends A
{
public static void main(
String[] args)
{
a.getInt(x);
}
}
now it should work fine try it.......................remember while using protected the only way you can access protected method and vaiable is through inheritance.......there is no other way out.............