can a static method be overridden
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is the difference between system.out ,system.err and system.in?
How many layers are there in RMI and what are they?
whats is mean by tiles in struts
advantage of thread?
What is clustering? What are the different algorithms used for clustering?
What is servlet preinitialization?
What happens when a thread cannot acquire a lock on an object?
what is a dirty read?
When a thread terminates its processing, it enters into what state?
What is Introspection?
Can you control when passivation occurs?
What are the JSP implicit objects ?