Why static methods cant be overridden in java?

mmk

New Member
#2
Re: overriding in static

Well,

Overriding will come to the picture with the objects i mean runtime. Static talks about the class and it is related to the class methods i mean static methods. Even if try to override the static method, the compiler didn't look the static method because it has been hidden and the result you won't get any compile time error and we will assume it has overrided but the bottom line is the static method has been hidden.
 
#4
The primary memory which is divided in to three types they are heap area,method area,stack area. The objects of java resides in heap area obuviously instance methods store in heap area. All static methods jvm will stores in th method area. when ever the class is loaded all the static methods are stored in the method area. In method area the jvm doesn,t allows the same type to store again.
Note : when ever the class is loaded first all the static variables and static blocks last static methods are stored in the jvm.And the important point to remember is static methods should be called with respect to class name.
 
#5
static methods can not be overridden
this is because method overridding occears in run time when objects are created.
but the static methods are loaded when first class is loaded, till that time there are no objects in memory,
 
Last edited:
#6
But it's possible :confused:

Consider below:

class MySuper
{
static void sd()
{
System.out.println("I ammmmm MySuper");
}
}
public class MySub extends MySuper

{
static void sd()
{
System.out.println(" I am MySub");
}


public static void main(String ...a)
{
MySub my=new MySub();
my.sd();

}
}
 
#7
class MySuper
{
static void sd()
{
System.out.println("I ammmmm MySuper");
}
}
public class MySub extends MySuper

{
static void sd()
{
System.out.println(" I am MySub");
}


public static void main(String ...a)
{
MySub my=new MySub();
my.sd();

}
}


/ it is call redefine /
 
#8
A example system can't override An static method, and a static technique can't hiddenite an occurrence strategy. To example, those Emulating project need two compiler errors. In An subclass (or inferred Class), we camwood over-burden the routines inherited starting with those superclass.
 
Top