0

On object of child class, static methos of super class are available but when we define same method in child class, now object of child class start pointing to child class method.this complete sounds like overriding but it is not,since static method can't override. How this is happen and what this functionality of java is called?

class A extends B {
    public static void main(String[] args) {
        new A().method();//call class B's method if method     is not in A otherwise A's
    }

    /*
    public static void method(){
    System.out.println("class A method");
    */
}

class B {
    public static void method() {
        System.out.println("class B method");
    }
}

This seems like overriding but not.how jdk manage it? I am sorry for the formet due to my rubbish tablet.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 2
    @Bubletan static methods cannot be overridden. – Luiggi Mendoza Apr 18 '15 at 20:54
  • 1
    @LuiggiMendoza Oh true, didn't see it was static as it was called from an instance. – Bubletan Apr 18 '15 at 20:54
  • Take a senerio same for private method, since in case of private it is not available to child class so implementation of child class is not overriding.but in case of static when implementation is not given in child class parent class method was available but not after when I give implementation in child. –  Apr 18 '15 at 21:03
  • @dubey-theHarcourtians if you question was answered, please remember to select an answer and close it. – Turing85 Apr 19 '15 at 09:28

1 Answers1

2

Since A extends B, an instance of A (which you create by calling new A()) will have all methods, which B has. Therefore, if you call .method() on an instance of A, the VM looks for a method() first in its own scope, i.e. the dynamic methods within A, then the dyanmic method within B, then the static methods within A and finally the static methods within B. This is possible because the VM allows accessing static methods via a this reference, although this is discouraged since it compromises readability.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • As u told VM looks first within child class than in parent ,this sounds like same as overriding (for non static methods).where it is different from overriding? –  Apr 19 '15 at 05:34