can a static method be overridden
Answers were Sorted based on User's Feedback
Answer / ramandeep
Yes you can override static methods..
see the following two classes :
public class Super
{
public static void main(String args[])
{
}
public static void m1()
{
System.out.println("superclass");
}
}
public class Sub extends Super
{
public static void main(String args[])
{
Super superWalaObj = new Sub();
superWalaObj.m1();
Sub subWalaObj = new Sub();
subWalaObj.m1();
}
public static void m1()
{
System.out.println("subclass");
}
}
Running this gives the following output :
superclass
subclass
Which explains the behaviour itself.
By overriding the static methods we are forcing them to
behave as non-static methods when used with object.
However, making a call directly to the staticy. method will
call the method belonging to that class only
Is This Answer Correct ? | 7 Yes | 1 No |
Answer / narasimha rao bodagala
no, static methods are not overriden cause the compiler
will load the class is loded by the jvm so the static we
can't overriden.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / vivek srivastav
yes static method can
bt only as a static method
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sushant
@ guys above.... if you don't know the exact vcorrect
answers don't put your guesses here so as to confuse people
further.
static method can NEVER be overridden.
Static method in a way are hidden and are called on the
Class itself not on the instance.You can do it but it
implicitly calls the static methods on the class.
Overriding doesn't just mean having the same method name in
the subclass as it is in the superclass. You can still have
the same method name in the subclass but it is not
overriding the superclass method and it is not polymorphism
as well. For more info please google on this.
static method can NEVER be overridden.
-Sushant
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / nilesh more
Finally, remember that static methods can't be overridden!
This doesn't mean they can't be redefined in a subclass, but
redefining and overriding aren't the same thing.
Let's take a look at an example of a redefined (remember,
not overridden), staticmethod:
class Animal
{
static void doStuff()
{
System.out.print("a ");
}
}
class Dog extends Animal
{
static void dostuff()
{ // it's a redefinition,
// not an override
System.out.print("d ");
}
public static void main(String [] args)
{
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}
Running this code produces the output:
a a a
The above written material is from kathe Siera which clearly
says that "Static methods can't be overridden."
But you can only redefine them which is not as overriding.
For any further perfect explanation please refer kathe Ciera
book
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / prabhu
static method can be overriden...absolutely its
possible...try it guys
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / prathyusha
Static Method methods are class methods (doesnot belong to
particular Object) so the concept of runtime polymorphism
doesn't arise here..
Staic void dostuff() method is actually redefined in
subclass but not overridden..
Hence Static methods can NEVER be overridden
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / vinay
Making things more clear, how static and non-static method
behaves
public class Super
{
public static void m1()
{
System.out.println("superclass static ");
}
public void m2()
{
System.out.println("superclass nonstatic ");
}
}
public class Sub extends Super
{
public static void main(String args[])
{
Super superWalaObj = new Sub();
superWalaObj.m1();
superWalaObj.m2();
Sub subWalaObj = new Sub();
subWalaObj.m1();
subWalaObj.m2();
}
public static void m1()
{
System.out.println("subclass static ");
}
public void m2()
{
System.out.println("subclass nonstatic ");
}
}
Result:
superclass static
subclass nonstatic
subclass static
subclass nonstatic
Note: The first output is not "subclass static" as with non
static methods.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / narasimha rao bodagala
Example:
class A {
static void something () {}
}
class B extends A {
static void something () {}
}
....
A anA = new B ();
anA.something ();
so, the compiler will not check the which one is to create
or which one is called method.
Is This Answer Correct ? | 0 Yes | 1 No |
Why use POJO when I can use hashmap
which book is better for jdbc ,servlets and jsp
Is System.err.println(""Statement"); is execute the output on console . Example: System.err.println("Hello JAVA");
what is an isolation level?
What is RMI and what are the services in RMI?
Is the session factory thread safe?
What is prototype?
Will the general public have access to the infobus apis?
which type of objects reference will be given to client?
What are the pros and cons of detached objects?
if i know the lenght of collection in hand, should I use Array or Arraylist? justify
What is glasgow?