java - Abstract Class - Why is my protected method publicly accessible? -


why can access dostuff() method in main method below? since dostuff() protected, expect testclassimplementation has access it.

public abstract class abstracttestclass {     protected void dostuff()     {         system.out.println("doing stuff ...");     } }  public class testclassimplementation extends abstracttestclass{  }  public class myprogram {     public static void main(string[] args) {         testclassimplementation test = new testclassimplementation();         test.dostuff(); //why can access dostuff() method here?     } } 

looks myprogram class in same package of abstracttestclass. if so, can access protected , public members of classes in same package.

covered in java tutorials:

modifier    class package subclass   world public      y     y       y          y protected   y     y       y          n no modifier y     y       n          n private     y     n       n          n 

in order fix this, move abstracttestclass package. similar other relevant classes.

more info:


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -