-3

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.

tcollart
  • 1,032
  • 2
  • 17
  • 29
DevX
  • 490
  • 6
  • 23

2 Answers2

2

For accessing protected method() , variables from class located in other package we need Inheritance. As i can see you have already used inheritance and inherited the class box of package pack1 correctly.

Now all except default and private members of that class aren't visible. What is visible to you the members those are declared as protected and public.

Now you can assume those methods are available in ur class. So, instead of calling method by object reference of that class will give you an error , while it won't give you any error if you call that method directly but in the constructor of a class ie Main() class in your case.

Why because,

There are two main restrictions for the static method.

They are:

1)The static method can not use non static data member or call non-static method directly.

2)this and super cannot be used in static context.

Try to call Hello1() method from class constructor. It will work.

Ravi Rajput
  • 539
  • 4
  • 12
0

The protected method Hello1() is defined in the Pack1, so you can't invoke it from another package - only from subclasses like this:

  Main main = new Main();      
   main.Hello1(); 

protected allows access from subclasses and from other classes in the same package. When you use your method static.

You might wonder "what's the reason for a static protected method, considering that it's impossible to call it from anywhere except from an instance of that class?". Usually there isn't much reason to declare a private or protected method as static. Static methods are usually public.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • But i have inherited it – DevX Jan 31 '16 at 08:36
  • but if i declare the method as static it is not giving me any error.protected static void Hello1() { System.out.println("Hello!!"); } – DevX Jan 31 '16 at 08:39
  • But thing which i am curious to know is why the access modifier is not working when we declare it static. – DevX Jan 31 '16 at 08:51
  • @DevX : Usually there isn't much reason to declare a private or protected method as static. Static methods are usually public. – Abdelhak Jan 31 '16 at 09:03
  • Thanks abdelhak ! I was trying to get the explanation to this behaviour.... Why after declaring static it is becoming accessible n without that it is not... – DevX Jan 31 '16 at 09:07