Understanding java's protected modifier

后端 未结 6 2165
感情败类
感情败类 2020-11-22 06:45

I have a class called A in package1 and another class called C in package2. Class C extends class A.

A has an instance variable which is declared like this:

<
6条回答
  •  无人及你
    2020-11-22 07:30

    No need to instantiate Protection class inside Protection2 Class. You can directly call the protected variable without instantiating the Protection class. Because Protection2 class extends Protection class. So variable automatically inherited by subclass.

    Try with below code:

    public class Protection2 extends Protection{
    Protection2()
    {System.out.println("n_pro = " +n_pro);
    }}
    

提交回复
热议问题