0

Can anyone explain, why can't I access protected variable in a subclass of another package? Testp2, OtherPackage, and Protection2 classes are stored in package p2, while Protection class is stored in p1 package. System.out.println("n_pro = "+p.n_pro); of Protection2.java shows error

package p2;

public class Testp2
{
    public static void main(String a[])
    {
                        // instantiating the classes of p1 package...
    Protection2 ob = new Protection2();     
    OtherPackage ob2 = new OtherPackage();
    }
}   


public class OtherPackage 
{
OtherPackage()
    {
    p1.Protection p = new p1.Protection();
    System.out.println("Inside SamePackage constructor.");

//  System.out.println(" n = "+p.n);                
//  System.out.println("n_pri = "+p.n_pri);

    System.out.println("n_pro = "+p.n_pro);
    System.out.println("n_pub = "+ p.n_pub);
    }
}


public class Protection2 extends p1.Protection{
    Protection2(){
    p1.Protection p = new p1.Protection();

    System.out.println("Inside SamePackage constructor.");
//  System.out.println(" n = "+p.n);                
//  System.out.println("n_pri = "+p.n_pri); 
//    System.out.println("n_pro = "+p.n_pro);   // when uncommented produces err(protected access specifier)
    System.out.println("n_pub = "+p.n_pub);
    }
}


package p1;

public class Protection{

int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;

public Protection(){
    System.out.println("Inside base constructor.");
    System.out.println(" n = "+n);
    System.out.println("n_pri = "+n_pri);
    System.out.println("n_pro = "+n_pro);
    System.out.println("n_pub = "+n_pub);
    }
}

The stacktrace is as follows:

C:\Users\jaina_000\Desktop\learn_java>javac p2/Testp2.java .\p2\Protection2.java:9: error: n_pro has protected access in Protection System.out.println("n_pro = "+p.n_pro); // when uncommented produces err (protected access specifier) ^ .\p2\OtherPackage.java:9: error: n_pro has protected access in Protection System.out.println("n_pro = "+p.n_pro); // when uncommented prod uces err(protected access specifier) ^ 2 errors

C:\Users\jaina_000\Desktop\learn_java>

Jainam Jhaveri
  • 1,479
  • 1
  • 18
  • 31
  • 1
    What error are you getting? – gprathour Dec 17 '14 at 05:28
  • It's a basics of oops protected variable can be accessed only in package see this. http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private – Key_coder Dec 17 '14 at 05:30
  • please specify OtherPackage and Protection2 are in which package – LMK Dec 17 '14 at 05:31
  • OtherPackage and Protection2 are in p2 package – Jainam Jhaveri Dec 17 '14 at 05:35
  • @Gaurav then what would be the difference between `protected` and `default (package public)`? – gprathour Dec 17 '14 at 05:37
  • I wonder you have extended the class `Protection2 extends p1.Protection` and still you create an object to access its members `p1.Protection p = new p1.Protection();`. Why so? – gprathour Dec 17 '14 at 05:38
  • @Gaurav: In the table mentioned, it shows protected variable is accessible in the subclasses of another package. In this code, Protection2 extends p1.Protection. So, should it(n_pro variable of class Protection) not be accessible in Protection2? – Jainam Jhaveri Dec 17 '14 at 05:39
  • C:\Users\jaina_000\Desktop\learn_java>javac p2/Testp2.java .\p2\Protection2.java:5: error: cannot find symbol Protection p = new Protection(); ^ symbol: class Protection location: class Protection2 .\p2\Protection2.java:5: error: cannot find symbol Protection p = new Protection(); ^ This is the stacktrace @GPRathour, when I try to compile using Protection p = new Protection(); – Jainam Jhaveri Dec 17 '14 at 05:44
  • I can give you one hint. `Protection2 extends p1.Protection` means that at location where `Protection2` class is saved, there should be another package `pi` having class `Protection`. Suppose Protection2 is on desktop then on desktop there should be a package p1 which should have Protection.class file. Now you will have to check your package structure. – gprathour Dec 17 '14 at 05:48

0 Answers0