I use Boyarskiy OCA certification book, page 173.
So, I faced with some issue:
I have parent class Bird
with protected
field name and child in other package.
package bird;
public class Bird{
protected String name;
}
and child:
package swan;
import bird.Bird;
public class Swan extends Bird{
public void check1(){
System.out.print(name);
}
public void check2(){
Swan swan = new Swan();
System.out.print(swan.name);
}
public void check3(){
Bird bird = new Bird();
System.out.print(bird.name);
}
}
So,
-check1
compile ok
-check2
compile, but why? check2()
is create new instance of class
and try to call parent field name from other package(not via inheritance).
-check3
doesn't compile.