Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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 is Stream and Types?

2 Answers   Patni,


function of extends and implements keywords?

2 Answers  


What is the diffrence between a local-tx-datasource and a xa-datasource?

0 Answers  


Will the general public have access to the infobus apis?

0 Answers  


which type of objects reference will be given to client?

0 Answers  






Name the method to find, if a thread is active or not?

2 Answers  


whats is mean by tiles in struts

0 Answers   SolutionNET,


Hello, I'm java developer. My skill is programming with java,jsp, struts, struts2+spring, hibernate, eclipse, tomcat, mysql,Oracle,Middlegen and other open source technology. I'm interest work any web base project that use my ability above. The thing is that I have never got any chance to work in this field though i am very much comfort with my skills. So if any body want me then i will happily join you. Also I have also some of my friends if you. If you interest to outsource any project to me then also you are welcome. you can reach me by email at me4bangalore@yahoo.in

4 Answers  


What is the difference between the ‘font’ and ‘fontmetrics’ class?

0 Answers  


whats is mean by class.forName() whats the return type of class

3 Answers   SolutionNET,


Name three subclasses of the component class?

0 Answers  


suppose i open my yahoo mail.then copy the url. then sign out & close browser.then open browser paste that url on the browser.will i get my mail opened or not? i know it ill not.but how it work.how i create like this type of secure site?

7 Answers  






Categories