Answer Posted / 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 |
Post New Answer View All Answers
what is an isolation level?
What happens when a thread cannot acquire a lock on an object?
What is the difference between the font and fontmetrics classes?
For which statements does it make sense to use a label?
What is metaspace?
What is the difference between RMI registry and OS Agent?
What is the infobus?
What is the purpose of the wait() method?
Difference between loadclass and class.forname?
Can I import same package/class twice? Will the jvm load the package twice at runtime?
what is a portable component?
What modifiers may be used with an interface declaration?
How many times may an objects finalize() method be invoked by the garbage collector?
What is synchronization and why is it important?
Explain the difference between object state and behavior?