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

What is multi-tasking? Types?

1 Answers  


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

0 Answers  


how team leaders assign work for the team member like through papers,word document

1 Answers   TCS,


What are JTA/JTS and how they used by client?

0 Answers  


What is a session? Can you share a session object between different theads?

0 Answers  






suppose i open my yahoo mail.then copy the url. then sign out & close browser.then open browser paste that url on the browser.will i get my mail opened or not? i know it ill not.but how it work.how i create like this type of secure site?

7 Answers  


What is the initial state, When a thread is created and started?

3 Answers  


What is the difference between session and entity beans?

0 Answers  


What event results from the clicking of a button?

0 Answers  


What is a class loader?

0 Answers  


What is TL and its use?

0 Answers  


What is JNI?

1 Answers   Google, TCS,


Categories