How private method of super class are resolved?

前端 未结 5 546
抹茶落季
抹茶落季 2021-01-02 22:50
class A{ 
  private void sayA(){
     System.out.println(\"Private method of A\");
  }
  public static void main(String args[]){
      A instanceA=new B();
      ins         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 23:34

    private means that only the class that declares the field can see it. Because you're calling instanceA.sayA(); from within class A the method is visible and the code both compiles and runs. If you were to try to call that method from within class B or any other class you would get the compile warning that The method sayA() from the type A is not visible

提交回复
热议问题