What does Java object assignment mean? -


i have following 2 classes :

class animal {     public static void staticmethod(int i) {         system.out.println("animal : static -- " + i);     }      public void instancemethod(int i) {         system.out.println("animal : instance -- " + i);     } }  class cat extends animal {     public static void staticmethod(int i) {         system.out.println("cat : static -- " + i);     }      public void instancemethod(int i) {         system.out.println("cat : instance -- " + i);     }      public static void main(string[] args) {         cat mycat = new cat();         mycat.staticmethod(1);                       // cat : static -- 1         mycat.instancemethod(2);                     // cat : instance -- 2         system.out.println("");          animal myanimal = mycat;         animal.staticmethod(3);                      // animal : static -- 3          myanimal.staticmethod(4);                    // animal : static -- 4 [ ? ]         system.out.println("");          myanimal.instancemethod(5);                  // cat : instance -- 5     } }  

and when run cat, got following results :

cat : static -- 1 cat : instance -- 2  animal : static -- 3 animal : static -- 4  cat : instance -- 5 

i can understand 1,2,3 , 5, why #4 not : " cat : static -- 4 " ? understanding :

myanimal=mycat means "myanimal" same "mycat", anywhere "myanimal" apears, can replace "mycat" , same result, because inside myanimal same inside mycat, therefore "myanimal.staticmethod(4)" should same "mycat.staticmethod(4)" , output should : "cat : static -- 4", similiar "mycat.staticmethod(1)" above.

but doesn't seem case, why ?

from oracle docs:

8.4.8.2. hiding (by class methods)

if class c declares or inherits static method m, m said hide method m', signature of m subsignature (§8.4.2) of signature of m', in superclasses , superinterfaces of c otherwise accessible code in c.

example 8.4.8.2-1. invocation of hidden class methods

a class (static) method hidden can invoked using reference type class contains declaration of method. in respect, hiding of static methods different overriding of instance methods. example:

class super {             static string greeting() { return "goodnight"; }             string name() { return "richard"; }         }         class sub extends super {             static string greeting() { return "hello"; }             string name() { return "dick"; }         }         class test {             public static void main(string[] args) {                 super s = new sub();                 system.out.println(s.greeting() + ", " + s.name());             }         } 

produces output:

goodnight, dick

because invocation of greeting uses type of s, namely super, figure out, @ compile time, class method invoke, whereas invocation of name uses class of s, namely sub, figure out, @ run time, instance method invoke.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -