can a static method be overridden
Answers were Sorted based on User's Feedback
Answer / lakshmanan n
Static methods can be overriden but why do you want to call
the static methods thro instance of the subclass or super
class.
If it is a static method.
call those methods like the following...
SuperClassName.methodName()
SubClassName.methodName()
| Is This Answer Correct ? | 2 Yes | 3 No |
Answer / nitasha
Static methods are not overriden, they are hidden by static
method in subclass. Check out the link:
http://www.xyzws.com/javafaq/can-static-methods-be-overridden/1
| Is This Answer Correct ? | 4 Yes | 6 No |
Answer / nisha
/* 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
dostuff();
Animal a=new Dog();
a.doStuff();
}
}
| Is This Answer Correct ? | 2 Yes | 4 No |
Answer / sandya
Hi, Narasimha Rao Bodagala
I didnt get u. can u please explain once?
| Is This Answer Correct ? | 3 Yes | 6 No |
Answer / mohit kaushik
yes main method can be override example
class mad
{
public static void main(String args[])
{}
}
public class mohit extends
{
public static void main(String args[])
{
System.out.println("HELLO");
}}
output HELLO
ANY question contact me with our email.my id.
mohit_kaushik1234@rediff.com
| Is This Answer Correct ? | 2 Yes | 6 No |
Answer / abid mehmood
An instance method can not override the static method of
its parent class.like
if parenet class contains
public static void test()
{
}
then in child class you can't override like this
public void test(){ //you can't override
}
or even like this
public static int test(){ //compile time error:type is
incompatibe with parent's test method.
}
| Is This Answer Correct ? | 4 Yes | 9 No |
Answer / karteek
Hi, Srinivas.....in the above program posted by Aswini...I
think main() method is overridden....right?.....then we can
say static methods can be overridden....
Eg:
class Parent
{
public static void myStaticMethod()
{
System.out.println("A");
}
public void myInstanceMethod()
{
System.out.println("B");
}
}
public class Child extends Parent
{
public static void myStaticMethod()
{
System.out.println("C");
}
public void myInstanceMethod()
{
System.out.println("D");
}
public static void main(String[] args)
{
Parent o1 = new Parent();
Child o3 = new Child();
Parent.myStaticMethod(); // A
Child.myStaticMethod(); // C
o1.myStaticMethod(); // A
o1.myInstanceMethod(); // B
o3.myStaticMethod(); // C
o3.myInstanceMethod(); // D
}
}
In the above program myStaticMethod() is
overridden.....which is a static method.....
| Is This Answer Correct ? | 10 Yes | 16 No |
what is the use of Object Factories?
What is the use of Class.forName
Give me simple example of hibernate caching and explain the details of caching????? thanks in adv.
Is jvm a overhead?
How can a dead thread be restarted?
What method MUST be implemented by all threads?
wahts is mean by dynavalidatorform in struts/
For an example, if we have some variable in run method, and we created one or more threads. Does all threads will share the same variable or a copy of variable is created for each thread??
What are preemptive scheduling and time slicing and what is the difference between them?
How to implement dphibernate to activate lazy loading in Flex with java ?thanx in advance!
What is multi-tasking? Types?
the use of try and ffinally keyword