Static methods are override or overload ?

Showing Answers 1 - 25 of 25 Answers

Abhishek

  • Oct 18th, 2006
 

Hi friends,

    You can override as well as overload any static methods. An instance method cannot override static method and a static method cannot hide  any instance method.If you do the same then compiler will give error.

                                                             Abhishek(Sharad)

  Was this answer useful?  Yes

Guest

  • Oct 18th, 2006
 

static method overload

  Was this answer useful?  Yes

AnilKumar

  • Nov 13th, 2006
 

Hi Mr. Abhishek

        Plz Check it once and give one example...

      Regards

      Anil

  

  Was this answer useful?  Yes

Hi Mr. Anil,

  I am sorry for i have written that static methods can be overriden but you can see the code written below i am agree we can not say static method can be overridden but if it follows all the rules for method overriding then we can say that it is hiding the method which is defined in the super class.A hidden static method can be invoked by using the superclass name in the subclass declaration. We can also call the super class method in non static code with super keyword. Please compile this code and give another if you get any error.

class Test
{
 static String Greet(String name)
 {
  String msg = "Good Morning! " + name;
  return msg;
 }
 static int Greet(int nm)
 {
  return (nm*nm);
 }
 public static void main(String[] args)
 {
  Test ts = new Test();
  System.out.println(ts.Greet("Abhishek"));
  System.out.println("Square value: " +ts.Greet(12));
 }
}
class Test2 extends Test
{
 static String Greet(String nm)
 {
  
  String msg = "Good Evening! " + nm;
  return msg;  
 } 

 public static void main(String[] args)
 {
  Test2 ts2 = new Test2();
  Test ts1 = new Test();
  System.out.println(ts2.Greet("Abhishek"));  
  System.out.println(ts1.Greet("Sharad"));
  System.out.println("Square value :" + ts1.Greet(2));
 }
}

                      Abhishek(Sharad)

  Was this answer useful?  Yes

chinnu

  • Nov 18th, 2006
 

hi,static method are overridden methods as well as overriding static methods .but we cant override static method s into instance methods.as well as viceversa.bye chinnu

  Was this answer useful?  Yes

Niraj Lavankar

  • Mar 12th, 2007
 

Hi before uploading the information please make sure the data.Static method never be overriden...

A static method cannot override an inherited instance method, but it can hide a static method if the exact requirements for overriding instance methods are fulfilled. A hidden superclass static method is not inherited. The compiler will flag an error if the signatures are the same but the other requirements regarding return type, throws clause, and accessibility are not met. If the signatures are different, the method name is overloaded, not hidden.

  Was this answer useful?  Yes

Amit Patil

  • Sep 19th, 2007
 

Static method can be overloaded. However if you try to override them the method of base class isnt hidden. The compiler gives no error if you try to override.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions