Why do these two code samples produce different outputs?

后端 未结 5 988
离开以前
离开以前 2021-01-18 02:57

Sample 1:

 class Animal {
     public static void saySomething() { System.out.print(\" Gurrr!\"); 
   }
 }
 class Cow extends Animal {
    public static void         


        
5条回答
  •  不思量自难忘°
    2021-01-18 03:04

    Static methods are tied to the "Class", not the "Instance" of the object. Since you are referring to an "Animal" and calling the static method saySomething(). It will always make the call to "Animal" unless you are referring to a Cow.

提交回复
热议问题