-3

Getting sightly confused...I read that static method does not override but in the below program its not following that rule...Can anyone please explain it in detail.Here is the program
The Output of program is class A method output.

public class JavaApplication6 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       A a=new B();
       a.r();
    }

}
class B extends A{
    static int d=200;
    static void r(){
        System.out.println("hiiiiiiiiiiiiiiiii r"+d);
    }
}
class A {
   static int a=10,c=20;
  static void r(){
        System.out.println("hiiii ar"+a);
    }
}
Roopam
  • 270
  • 2
  • 12
  • if the output is from A's method, then it's not overrided. – Hacketo Jul 10 '15 at 08:21
  • On my laptop your program produced 'hiiii ar10', which means the A.r() works. So the result did show that no overiden happens for static method. – Harry.Chen Jul 10 '15 at 08:22
  • The keyword `extend` means inheritance, not overriding. Besides - the two classes are not `static` - they have `static` methodes, but the classes themselves are not static. – TDG Jul 10 '15 at 08:22
  • 1
    possible duplicate of [What is method hiding in Java? Even the Javadoc explantion is so confusing.](http://stackoverflow.com/questions/16313649/what-is-method-hiding-in-java-even-the-javadoc-explantion-is-so-confusing) – Rajesh Jul 10 '15 at 08:33
  • if you add @Override, then the problem might show – Mackiavelli Jul 10 '15 at 08:45
  • See the link provided by Rajesh. Read about static methods hiding in Official documentation https://docs.oracle.com/javase/tutorial/java/IandI/override.html – RahulArackal Jul 10 '15 at 08:48

3 Answers3

0

Static methods are never overridden by child classes. If you put the @Override annotation it will give a compilation error that you can't override it telling to remove the annotation.

"The method method1() of type Child must override or implement a super type method"

It is inherited though to the child class.

RoHaN
  • 1,356
  • 2
  • 11
  • 21
0

Static methods can not be overridden, but it can be hide parent static methods

For a neat example have a look here.

And this is java documentation explaining the difference between overriding instance methods and hiding class (static) methods.

Overriding: Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it (which is the case with overriden static methods)

Hiding: Parent class methods that are static are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical to the one in its parent class, this subclass static method is unique and distinct from the static method in its parent class.

for more details click here hope it'll help you.

Mahesh Shukla
  • 186
  • 10
0

Static Method cannot be overridden, the code you have present is a example of Method hidding. In the above example your class B's method is hidden using static keywork.

Ankush soni
  • 1,439
  • 1
  • 15
  • 30