-3

I have a Super-class A in PackageOne and a Sub-class B which is in PackageTwo.

package PackageOne;

public class A {        
protected number = 0;        
}

Second Class is here

package PackageTwo;

public class B extends A {
    public static void main(String args[]) {
       A a = new A();
       System.out.println(a.number);// Here is the error occurs        
    }        
}

Error is The field Person.name is not visible;

As I know a protected member can be accessed by a super-class reference, but why the error occurs.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Ahsan Rafiq
  • 111
  • 2
  • 8

4 Answers4

2

Controlling Access to Members of a Class

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Because it is protected member. You need to extend (inheritance) the class to access the protected members of a class in another package.

Instead of accessing via A access it via B. Once you extend the class A it becomes the part of class B. You cannot access it using the reference of A.

public class B extends A {
  public static void main(String args[]) {
    B b = new B();  // create the instance of B.
    System.out.println(b.number);  // access via the b.
  }
}

You are also missing the data type in class A:

public class A {
  protected int number = 0;
}

In the same package you can reference the members using the instance reference. Check the below code:

package PackageOne;

public class C {
  public static void main(String[] args) {
    A a = new A();
    System.out.println(a.number); // this works.
  }
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
2

If you are in a different package, you can't access the field of another instance. You can only access the field within yourself.

Class A:

package a;

public class A {
    protected int i;
}

Class B:

package b;

public class B extends A {

    void m() {
        A a = new A();
        System.out.println(a.i); // error here!

        System.out.println(i);   // but no error here
    }
}

If A and B were in the same package, though, there would be no error whatsoever.

Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
1

Protected methods can only be accessible through inheritance in subclasses outside the package. And hence the second approach tryMeProtected(); works.

The code below wont compile because we are not calling the inherited version of protected method.

Example
Class1 c = new ClassA();
c.tryMeProtected(); // ERROR: tryMeProtected() has protected access in ClassA

Follow This stack link for more explaination.

Community
  • 1
  • 1
Aniket Karne
  • 325
  • 3
  • 14
0

That's not how inheritance works. You don't make a class a subclass of another one by creating an instance of it.

You have to extend it.

public class B extends A

This will allow you to access protected fields. However, you will have to import PackageOne.A to do this.

EDIT

See this SO answer by @David Segonds on how these keywords work

            | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    y     |   n

You have to be a subclass or in the same package to access these properties.

From the official documentation:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44