can a static method be overridden

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


Please Help Members By Posting Answers For Below Questions

What is re-entrant. Is session beans reentrant. Is entity beans reentrant?

589


What are the pros and cons of detached objects?

550


Can I map more than one table in a cmp?

596


How primary key is implemented in Oracle?

1920


What event results from the clicking of a button?

700






What is mdb and what is the special feature of that?

571


What is the infobus?

615


When a thread blocks on i/o?

573


What are the types of scaling?

560


What is the difference between the session.get() method and the session.load() method?

604


what are RemoteObjects?

2262


Name three subclasses of the component class?

619


Can I run seam with jdk 1.4 and earlier?

561


Is the infobus client side only?

615


Write a program to show synchronization?

696