Protected constructor and accessibility

后端 未结 4 1807
余生分开走
余生分开走 2020-11-30 03:52

Why can\'t we instantiate a class with a protected constructor if its child is in a different package? If protected variables and methods can be accessed, why doesn\'t the s

4条回答
  •  我在风中等你
    2020-11-30 04:36

    I agree with previous posters, don't know why you would want to do this (instantiate parent in that way in extending class) but you could even do something like this:

    public void test() {
        A obj = new A(){}; // no compilation error; why? you use anonymous class 'override'
        ...
    

提交回复
热议问题