can a static method be overridden
Answers were Sorted based on User's Feedback
Answer / vinay
Hi pals,
I agree with sushant who says that method overriding is not just using same name for the method.
But I want make some things more clear.
first of all Whats Method overriding , giving a new implementation for the parent or base class methods mean while maintaining same method definition.
If thats the case
You can override static methods by all means.
In this case only thing differs is way of accessing the methods , as its static access it by their class name.
And also
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / ashish
Hi All,
static method can never be overridden, to clear your doubt
I have written the following two java files
Base.java -->
public class Base {
public static void method(){
System.out.println("base");
}
}
Child.java -->
public class Child extends Base {
public static void method(){
System.out.println("Child");
}
public static void main(String[] args) {
Base baseObj = new Child();
baseObj.method();
baseObj = new Base();
baseObj.method();
}
}
The class file for Child.java is:
public class Child extends Base
{
public Child()
{
}
public static void method()
{
System.out.println("Child");
}
public static void main(String args[])
{
Base baseObj = new Child();
Base.method();
baseObj = new Base();
Base.method();
}
}
As you can see in case of static method object got replaced
by Class Name(here Base).
And i believe you all are aware of fact that overridden is
the case when resolution is done at Runtime.
But since in our case calling is decided at compile time,
so there is no case of overridden.
Do let me know, if you have further query
| Is This Answer Correct ? | 0 Yes | 1 No |
What is message driven beam?
cud u help me ... i am struggling with this question... to find all the subsets of a given set for ex.... a,,b,c shud give all the subsets.... i gt the program in c bt nt able to get it in java..... help needed ..
Difference Between java & javax
5 Answers Sun Microsystems, Wipro,
What is a clone?
if i know the lenght of collection in hand, should I use Array or Arraylist? justify
Explain the different types of memory used by jvm?
What is the use of Class.forName
Is infobus easy to use?
What state does a thread enter when it terminates its processing?
What is synchronization?
What is the difference between long.class and long.type?
when we applied start()method on a thread ,how does it know that to execute run()method on that object?