Java protected methods access from different instance -
i extending class package when found wasn't compiling, though thought should compile.
i have 2 classes, in different packages. in package com.foobar.a:
package com.foobar.a; public class { protected void foo1() { system.out.println("foo1() called!"); } protected static void foo2() { system.out.println("foo2() called!"); } }
and in package com.foobar.b:
package com.foobar.b; import com.foobar.a.a; public class b extends { public void bar() { obj = new a(); obj.foo1(); // doesn't compile a.foo2(); // compile } }
now, according this: http://docs.oracle.com/javase/tutorial/java/javaoo/accesscontrol.html subclass should able access method, if it's outside same package (and see method being static works). in fact, programming in eclipse , suggestion it's giving me fix issue "change visibility of 'foo1()' protected", it's protected.
so, what's going on here? in oracle specifications indicates access level using class, package, subclass , world. should add "instance" list, , if so, rules be?
if want access method in subclass, can use this:
public void bar() { this.foo1(); }
creating object , trying access protected method isn't accessing super class protected method.
Comments
Post a Comment