can a static method be overridden

Answers were Sorted based on User's Feedback



can a static method be overridden ..

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

can a static method be overridden ..

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

Post New Answer

More Advanced Java Interview Questions

How will the struts know which action class to call when you submit a form?

6 Answers   HeadStrong,


which deployment descriptor element is used to configure the authentication method? a. auth-config b. login-config c. sec-config

1 Answers  


What is the use of Class.forName

20 Answers   HCL,


what is a non-repeatable read?

0 Answers  


hi friends, i have done my BE(CSE)at 2011 then i have joined one company before they asked me 50k so i paid but now they telling no project here so we r going to close like that so now i need any job i have good knowledge in core java and j2ee(jsp,servlet,jdbc) so if u know any job pl help me

0 Answers   Infosys,






Name the method to find, if a thread is active or not?

2 Answers  


difference between HashMap, Hashset and hashTable?

2 Answers   HCL, Infotech,


Explain ioc concept?

0 Answers  


Where can I ask questions and make suggestions about seam?

0 Answers  


What value does readline() return when it has reached the end of a file?

0 Answers  


What is local interface. How values will be passed?

0 Answers  


What are the difference between RMI and CORBA?

0 Answers  


Categories