Java: cannot access a protected member of the superclass in the extending subclass

后端 未结 5 839
野性不改
野性不改 2020-12-18 04:06

I want some discussions about this, but I could not infer the answer for my case. Still need help.

Here is my code:

package JustRandomPackage;

publi         


        
5条回答
  •  情话喂你
    2020-12-18 04:18

    Your code will work if YetAnotherClass will be in the same package as ATypeNameProgram. As others wrote it won't work in other cases. Here is the working example.

    package my.example;
    
    public class MainClass extends MyAnotherClass {
        public static void main(String[] args) {
            MyAnotherClass bill = new MyAnotherClass();
            System.out.println(bill.value); // this will work
        }
    }
    
    package my.example;
    
    public class MyAnotherClass {
    
        protected int value = 5;
    
    }
    

提交回复
热议问题