Why we can not override static method?

Questions by sachinTest

Showing Answers 1 - 35 of 35 Answers

this is wrong we can override a static method at the same time can also overload the staic method..

check out this example

 class Asd{
public static void method1(){
System.out.println("in CheckStatic class");
}
}
class Abv extends Asd{
public static void method1(){
System.out.println("in class CheckStatic1");
}}
class Imp{
public static void main(String prms[]){
Asd.method1();
}}

and to overload check this out

class Dq{
public static void method1(int s){
System.out.println("Overloaded method1 with one int argument");
}
public static void method1(int f, int d){
System.out.println("overloaded method1 with two int arguments");
}
}
class Yui{
public static void main(String args[]){
Dq.method1(9,3);
Dq.method1(8);
}}

Ramdas

  • Oct 5th, 2006
 

You cannot override static methods. Try the following.

class A{

public static void m1(){

System.out.println("m1 from A");

}

}

public class B extends A{

public static void m1(){

System.out.println("m1 from B");

}

public static void main(String args[]){

A a = new B();

a.m1();

}

}

IF the method was overridden then output should have been "m1 from B", but the output is "m1 from A"

ashish_setia

  • Oct 9th, 2006
 

 Dear see this is a simple concept. Static methods or variables are class level not instance level(DO Remember).

now in ur program u actually created the object of class  B and ur passing this object to the Reference of CLASS A.

So now the method which is accessed is of class A only.

Method overriding in case of non static method would always look for class whose object is made.

Sorry, i applog.  we cannot override a static method, but we r just shadowing its super implementation . thats it so its just method shadowing that s it

thanx

regards

ashish 

  Was this answer useful?  Yes

raJEEV

  • Oct 18th, 2006
 

 BECAUSE static method  call by class name reference rather than object of perticular class .so only one class that define this method ,can call it .

  Was this answer useful?  Yes

singhvishal

  • Aug 14th, 2007
 

Because static method binding is done at compile time not at run time but static variable binding is done at run time not at compile time. That is the reason that static method are not participated in overriding process.

Because static method is related to class...&...in overriding we use the instance to override. so when we want to override the static method they become as method hide.

  Was this answer useful?  Yes

Ganga_busi

  • Oct 26th, 2012
 

The static method is public final method , which is called by JVM directly before initializing the instances.
thats why we create our java main method (ie., public static void main(String args[])) as a static and this method is in side the user defined class.

  Was this answer useful?  Yes

Muhammed Younus Attari

  • Nov 28th, 2012
 

For calling every method either we need an Object or ClassName,
static is one which can be called directly, main is defined as static because it can be called directly by JVM

  Was this answer useful?  Yes

sushil

  • Nov 4th, 2015
 

We can override the static method, generally in real time we are not doing this. e,g

Code
  1. public class X {

  2. public static void m1(int i){

  3.         System.out.println("X static ");

  4. }

  5. }

  6.  

  7. public class Y extends X {

  8.         public static void m1(int i){

  9.                 System.out.println("Y  static ");

  10.         }

  11.         public static void main(String[] args) {

  12.                 Y y=new Y();

  13.                 y.m1(10);

  14.         }

  15. public class Y extends X {

  16.         public static void m1(int i){

  17.                 System.out.println("Y  static ");

  18.         }

  19.         public static void main(String[] args) {

  20.                 Y y=new Y();

  21.                 y.m1(10);

  22.         }

  Was this answer useful?  Yes

Ramesh

  • Feb 4th, 2016
 

If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. You didn't override the method a(), because static methods are not inherited. If you had put @Override, you would have seen an error.

  Was this answer useful?  Yes

Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time). Method overriding happens in the type of subtype polymorphism that exists in languages like Java and C++.

  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