inheritance - Why does java gives an error while just writing package-private member of class in subclass -
i have 2 classes r , t in package accessing package-private member of class r in t r.x; gives error while works system.out.println(r.x); why ? know r.x; useless still curious why so.
adding code sample
package test; public class main { public static void main(string[] args) throws interruptedexception { r obj=new r(); system.out.println("calling public method of z"+obj.getz()); system.out.println("calling protected method of y"+obj.gety()); system.out.println("calling static public member of z"+r.z); system.out.println("calling static protected member of y"+r.y); t.x;//this here shows error in eclipse r.z; system.out.println(t.x); // works fine } } package test; public class t { public final static int z=1; protected static int y=2; static int x=3; public int getz(){ return z; } protected int gety(){ return y; } private int getx(){ return x; } } package test; public class r extends t{ }
the private data members not accessible outside class declared/defined if access private data members happens class within same package. refer access rules data members here http://javapapers.com/core-java/access-modifiers-in-java-explain/.
Comments
Post a Comment