can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

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

can a static method be overridden ..

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

can a static method be overridden ..

Answer / vivek srivastav

yes static method can
bt only as a static method

Is This Answer Correct ?    1 Yes 0 No

can a static method be overridden ..

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

can a static method be overridden ..

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

can a static method be overridden ..

Answer / ravi

any one reply the answer with ex

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / prabhu

static method can be overriden...absolutely its
possible...try it guys

Is This Answer Correct ?    1 Yes 1 No

can a static method be overridden ..

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

can a static method be overridden ..

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

can a static method be overridden ..

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

Post New Answer

More Advanced Java Interview Questions

What are JTA/JTS and how they used by client?

0 Answers  


How database connectivity in XML is achieved?

0 Answers  


What are the high-level thread states?

1 Answers  


What is the applet sandbox?

1 Answers  


To what value is a variable of the string type automatically initialized?

0 Answers  






What is clustering? What are the different algorithms used for clustering?

0 Answers  


Will it be called overriding if I do not change the parameters or return type, instead throw a different exception in the method signature.

4 Answers   HeadStrong,


How to add new JTabbed pane?

1 Answers  


Difference Between java & javax

5 Answers   Sun Microsystems, Wipro,


What is the difference between the font and fontmetrics classes?

0 Answers  


What is colon_pkg_prefixes and what is its use?

0 Answers  


How has the sandbox changed with Java 2?

2 Answers  


Categories