can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

Answer / rama devi

Static method will be represented by class, not by objects.
Overriding technique will be for objects; Even if we have
same method in the sub class also that will be a static
method for that class

Is This Answer Correct ?    31 Yes 8 No

can a static method be overridden ..

Answer / srinivas katta

Hi, Aswini ur program is wrong, there is no relation btween
cal methods in ur program, overriding means there must be
same no of arguments,there is a change in no of argumetns
in ur program.
static methods can not be overridden
ex:
main method is static, we can overload it but we can not
override it.

Is This Answer Correct ?    27 Yes 5 No

can a static method be overridden ..

Answer / narasimha rao bodagala

Hi rama devi,u are written the answer is ok but not clear
i given one example of this

what i am saying is the static methods are not overriden
cause the class loader will load the while loading the
class at runtime so the compiler will check the which
static method would i need to call co its confused thas's
why the static methods are not overriden.

EXAMPLE:

class A {
static void something () {}
}

class B extends A {
static void something () {}
}

....
A anA = new B ();
anA.something ();

think and compile what's the comipler say's.

Is This Answer Correct ?    18 Yes 9 No

can a static method be overridden ..

Answer / rrr

Static methods cannot be overridden
Static methods can only be hidden
Instance method call binding is done as per the object's type
Static methods call binding is done as per the reference's
type( regardless of whether it contains an object of a
subclass or not), quite similar to field binding.

Is This Answer Correct ?    8 Yes 0 No

can a static method be overridden ..

Answer / kiran sangeri

no,bcos look at below code

package test;

class A {
static void something () {
System.out.println("class A");
}
}

class B extends A {
static void something () {
System.out.println("class B");
}
}


public class ClassC {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A anA = new B ();
anA.something ();
}

}

Output : class A

so Class A is not been overridden.

Is This Answer Correct ?    6 Yes 0 No

can a static method be overridden ..

Answer / partha

@ Mayank

Static methods are *NEVER* ever be overridden, even if you
try to override a static method of the super class in your
subclass, it will compile because in this you are simply
redeclaring the static method of your superclass in your
subclass. Redeclaring a method is not the same as overriding
a method.

The bottomline is

** STATIC METHODS CAN NEVER BE OVERRIDDEN **

Is This Answer Correct ?    8 Yes 5 No

can a static method be overridden ..

Answer / let the code speaks....

* STATIC METHODS CANT BE OVERRRIDDEN....*

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

Is This Answer Correct ?    9 Yes 6 No

can a static method be overridden ..

Answer / ravi ranjan

Yes a static method can be overridden
see following two classes

public class Test extends Test2{

public static void main(String args[]){
String str="";
int b=Test.cal(4);
System.out.println(b);
Test t =new Test();
t.displayX();
}
public static int cal(int a){
int c=a*a;
System.out.println("Hi I am Child" );
return c;
}
}

class Test2 {
public static void main(String args[]){

}

public static int cal(int a){
int c=a*a;
System.out.println(c);
return c;
}

public void displayX(){
System.out.println("HI i am in Super");
}

}

Compile this code there is no compile or run time error

Is This Answer Correct ?    3 Yes 0 No

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 onl

Is This Answer Correct ?    9 Yes 7 No

can a static method be overridden ..

Answer / konthoujam dhanabir singh

static method cannot be overriden to non-static.so static
method can be overriden to static.
the above example is true in static way
e.g.

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
Some case:
in the subclass Dog, if the method dostuff() is not
static,it will be compile time error in the above code
block .
So a static method cannot be overriden

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Advanced Java Interview Questions

What are transaction attributes?

0 Answers  


What is a session? Can you share a session object between different theads?

0 Answers  


Name the class that is used to bind the server object with RMI Registry?

0 Answers  


What is J2EE?

2 Answers  


What are the different types of exception?

0 Answers  






if i know the lenght of collection in hand, should I use Array or Arraylist? justify

1 Answers   iTrust,


In a multitiered application which tier is the browser in?

10 Answers   Adobe,


what is activation monitor and what is its job?

1 Answers  


what is a portable component?

0 Answers  


What is checkpoint? How to create checkpoints in our java projects?

1 Answers   ABC,


Which javutil classes and interfaces support event handling?

0 Answers  


diff between jsp include directive and jsp action include?

2 Answers   SolutionNET,


Categories