Can static methods be overridden?

Answers were Sorted based on User's Feedback



Can static methods be overridden?..

Answer / sonu

No never we cannot override the static methods becouse overriding is done only run time and static methods get memory at compile time and static things not the part of object. 

Is This Answer Correct ?    19 Yes 4 No

Can static methods be overridden?..

Answer / sumit pal singh

static class Class1 {
    public static int Method1(){
          return 0;
    }
}
static class Class2 extends Class1 {
    public static int Method1(){
          return 1;
    }




}
public static class Main {
    public static void main(String[] args){
            
          Class1.Method1();
          Class2.Method1();
    }
}

Is This Answer Correct ?    3 Yes 1 No

Can static methods be overridden?..

Answer / coder

Remember that static methods can't be overridden! This doesn't mean they
can't be redefined in a subclass, but redefining and overriding aren't the same thing.
Let's take a look at an example of a redefined (remember, not overridden), static
method:


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
*/

Here redefining means that you declare a static method with the same signature as the supper class'. So the same method in super class is redefined in the subclass. As you already know that the static methods can't be overridden so this is what you can do if you want the static method to behave differently in the subclass. But remember you can't have the polymorphism since no overridden happens.

Is This Answer Correct ?    1 Yes 1 No

Can static methods be overridden?..

Answer / durga

We can't override static methods,becoz static methods are loaded at class loading time,so it is class level.but we overload static methods.

Is This Answer Correct ?    0 Yes 0 No

Can static methods be overridden?..

Answer / deepak sikka

yes

Is This Answer Correct ?    4 Yes 7 No

Post New Answer

More Core Java Interview Questions

What is the class in java?

0 Answers  


Does constructor be static?

0 Answers  


What is difference between checked and unchecked exception in java?

0 Answers  


public class BatchTest { public static void main(String[] args) { Runtime run = Runtime.getRuntime(); try { Process p = run.exec("cmd start /c D:/test.bat"); System.out.println(p.exitValue()); } catch (Exception e) { e.printStackTrace(); } System.out.println("FINISHED"); } }

0 Answers  


Explain the difference between runnable and callable interface in java?

0 Answers  






Hi Friends, can u give me Real Time example for interface and abstract class.(With Banking Example)

4 Answers  


Explain about serializable interface in java?

0 Answers  


What is a singleton in genetics?

0 Answers  


Q) I have a ArrayList object, in that object i have added 5 integer values, 5 float values, 5 string values. Now question is how can delete particular type of data ( i.e all int values or all float values or string values) in that list object at a time?

3 Answers   Aricent,


how can i cal servlet from jsp page?how can i pass variablesfrom the jsp that servlet can access?

1 Answers   Verizon,


can abstract class have constructor how can you achive this ?

4 Answers   Fidelity,


What are the names of interfaces that doesn't consists of method/s ?

3 Answers  


Categories