I have a class in a different package where i declared a protected member. as shown
package Pack1;
public class Box {
public Box()
{
System.out.println("Box Class Contructor");
}
protected int x = 1;
protected void Hello1()
{
System.out.println("Hello!!");
}
}
Now i am extending this class to another package to call its protected member as shown:
public class Main extends Pack1.Box {
public Main()
{
System.out.println("main constructor");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Main main = new Main();
main.Hello1(); // not giving me any error
Pack1.Box b = new Pack1.Box();
b.Hello1();//Giving me an error
}
}
I am not sure why b.Hello1()
is giving me an error inspite of being inherited. But if i declare the Hello1()
as protected static void
it is not giving me an error.