can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

Answer / twinkle

no static methods cannot be overridden
but can be overloaded
for example in servlets,
Connection con=DriverManager.getConnection
(url,username,password);for all databases like oracle etc..
it can be overloaded as
Connection con=DriverManager.getConnection(url);//for MS
Access

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / dinesh

Heloo oops ,Answer in one word yaar,If they ask to explain
in detail than explain like above.

No-Static method can't be overridden.
From static method if u want to call another method ,U can
only call the static method,
Static method calls only the another static method.

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / karthik narayanan

Basically there are two concepts, one is overriding and the
other is hiding. When a subclass implements the same static
method as its parent class, then subclass will basically
hide the parents static implementation. So when you call
subclasses static method, only subclass method will get
executed. To execute the parents static method you have to
explicitly call it. But with instance method, you can hide
as well override the parent implementation, override in the
sense you can call the parent implementation by
super.dosamemethod()...

Hopefully this clears the debate..

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / karthik narayanan

also when you implement the same static method, in both
parent and subclass , and if you an instance of subclass and
say if your instance.thatstaticMethod(), the subclass
version will execute, but now if you typecast subclass to
the parent class and then execute
parentInstance.thestaticMethod() the parents version will
execute. this is different from overriding where even if
you typecast , the subclass version of the method will
execute. So basically, you can hide a static method
execution from a subclass, but you cannot override it.
understand???

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / raghvendra

Look at the code and resule below and Interpret it
yourself .. :) .. it is pretty easy.

public class A {
public static void staticMethod(){
System.out.println("Static: I print from
A");
}

public void nonStaticMethod(){
System.out.println("Non Static: I print
from A");
}

}

public class B extends A{
public static void staticMethod(){
System.out.println("Static: I print from
B");
}

public void nonStaticMethod(){
System.out.println("Non Static: I print
from B");
}
}

public class Launcher {

public static void main(String[] args) {
A a = new A();
B b = new B();
A obj = new B();

System.out.println("obj instanceof A : " +
(obj instanceof A));
System.out.println("obj instanceof B : " +
(obj instanceof B));

a.staticMethod();
a.nonStaticMethod();

b.staticMethod();
b.nonStaticMethod();

obj.staticMethod();
obj.nonStaticMethod();

}
}

Consol Output:

obj instanceof A : true
obj instanceof B : true
Static: I print from A
Non Static: I print from A
Static: I print from B
Non Static: I print from B
Static: I print from A <--- See the difference here.
Non Static: I print from B <--- See the difference here.


Good luck!

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / rohit salecha

Overriding is Dynamic Polymorphism , i.e. the method will
be called at run-time and not at compile time. and static
methods are called at compile time(Hence no Compiler Error).
So if you are overriding a static method , you defeat the
purpose of having having a dynamic binding.

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / naresh

static methods cant be overridden because let us consider
the following program
class Base
{
static void x(){}
}
class Derieved extends Base
{
@Override
static void x(){}
}
will produce compile-time error

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / stranger

static method cannot be overridden.....bcoz the method is define by class not by object.....

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / naveen

Abid Mehmood you are absolutely correct. You can't override a static method to a non static method.

Static method can be overridden unless the method is declared final.

Is This Answer Correct ?    0 Yes 0 No

can a static method be overridden ..

Answer / pradeep chawla

static methodes never be overridden because for static
variables memory alloted in jvm stack,for instance variables
memory will allotted in heap

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Advanced Java Interview Questions

i want documentation of text file splitter & merger of advanced java

1 Answers   Infosys,


What is a layout manager?

2 Answers  


we use MainFrame and using os390 for operating system with DB2 data base in IRAN and interest programing with java and use webspere for world wide,please help me where i should start?

0 Answers   IBM,


the advantages of polymorphic

1 Answers  


diff between jsp include directive and jsp action include?

4 Answers   SolutionNET,






important features of java which differenciate it from c++

3 Answers  


How do you iterate in Hashmap?

1 Answers   HCL, Infotech,


What is there in browser that it supports web based applications of java or any other PL?

2 Answers  


What is the highest-level event class of the event-delegation model?

0 Answers  


Differences between applications and applets?

3 Answers  


In a multitiered application which tier is the browser in?

3 Answers   Adobe,


What is waiting state? In what ways a thread can enter into waiting state?

2 Answers   Wipro,


Categories