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

后端 未结 5 833
野性不改
野性不改 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:26

    You aren't creating an instance of the class that extend it, but of the parent class. Check the code below:

    public class ATypeNameProgram extends YetAnotherClass{
        public static void main(String[] args) {
    
            YetAnotherClass bill = new YetAnotherClass();
            System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible
    
            ATypeNameProgram a = new ATypeNameProgram();
            System.out.println(a.variable); //this will work
    
        }
    }
    

提交回复
热议问题