Can we override static methods?

Answers were Sorted based on User's Feedback



Can we override static methods?..

Answer / sadheez

It may seems to be overriding the static methods, but the
real fact is HIDING.

class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");

}

public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}

class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");

}

public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}

class StaticHiding {
public static void main(String[] args) {
Foo f = new Bar();

f.instanceMethod();
f.classMethod();


when u run this program output will be:

instanceMethod() in Bar
classMethod() in Foo.

Here if u say it to be overriding then the
subclass ie., Bar class having static classMethod() should
be executed. But the fact here is Foo class static
classMethod() is executed.

So its method HIDING and not method
overriding..

I hope i have given answer to my best if
anyone feels wrong plz do post ur suggestions..

Is This Answer Correct ?    59 Yes 1 No

Can we override static methods?..

Answer / venkateswara reddy

Yes, V can override static methods
But overridding and overridden methods must static

Is This Answer Correct ?    51 Yes 16 No

Can we override static methods?..

Answer / yogesh gandhi

Neha,

I didn't get your answer.
You can overload as well as override static methods, but
only with static methods.

I have tried it and it is getting compiled.

Example is here.
*********************************************************
public class abc
{
public static void main(String args[])
{

}
public static void trss()
{

}
public static void trss(int i)
{
}

}
*********************************************************

Is This Answer Correct ?    13 Yes 1 No

Can we override static methods?..

Answer / madan mohanp

we cannot override a static method but we can overload a
static method.
Ex: override is not possible
class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");

}

public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}

class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");

}

public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}

class StaticHiding {
public static void main(String[] args) {
Foo f = new Bar();

f.instanceMethod();
f.classMethod();


when u run this program output will be:
instanceMethod() in Bar
classMethod() in Foo.

Ex: overload is possible
public class abc
{
public static void main(String args[])
{

}
public static void trss()
{

}
public static void trss(int i)
{
}

}

Is This Answer Correct ?    15 Yes 4 No

Can we override static methods?..

Answer / guest

YES, WE CAN OVERRIDE STATIC METHODS BY STATIC METHODS

Is This Answer Correct ?    25 Yes 16 No

Can we override static methods?..

Answer / karun

yes ,we can override the static methods to static methods
only.
sorry for the previous my wrong answer.:((((

Is This Answer Correct ?    23 Yes 15 No

Can we override static methods?..

Answer / yogesh gandhi

Thanks for addition.
You are correct.

Static method cannot be overridden.
Overloading of static methods is possible.

Is This Answer Correct ?    7 Yes 0 No

Can we override static methods?..

Answer / neha jain

dear yogesh
sory for wrong answer we can not override static method
with static method that known as method hiding.plz view
following example
Briefly, when you override a method, you still get the
benefits of run-time polymorphism, and when you hide, you
don't. So what does that mean? Take a look at this code:

class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");
}

public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}

class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");
}

public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}

class Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
If you run this, the output is
instanceMethod() in Bar
classMethod() in Foo

Is This Answer Correct ?    7 Yes 0 No

Can we override static methods?..

Answer / n.suresh babu

Yes, Static Methods can be overridden but it won't give
overridden behaviour. Overiding execution happens based on
the runtime object.

Is This Answer Correct ?    16 Yes 10 No

Can we override static methods?..

Answer / edward sudhaharchennai

Static methods can be hidden it cannot be overriden

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is java console application?

0 Answers  


1).Is Object class abstract or not? 2).Is main method(public static void main(String args[])low priority thread or high priority thread?

3 Answers   TCS,


What happens if we don’t define serial version uid?

0 Answers  


What do you know about the garbage collector in java?

0 Answers   TCS,


Can a method be overloaded based on different return type but same argument type?

0 Answers  






Differences between GridLayout and GridBagLayout?

1 Answers  


Explain how hashmap works?

0 Answers  


Can we override private method?

0 Answers  


What is runtime polymorphism or dynamic method dispatch?

0 Answers  


what is use of business objects?

3 Answers   Tech Mahindra,


11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B”); } 16. } 17. public static void main(String[] args) { 18. new B().process(); 19. } What is the result? 1 B 2 The code runs with no output. 3 Compilation fails because of an error in line 12. 4 Compilation fails because of an error in line 15.

6 Answers  


can any one tell me when do u go for inheritance and polymorphism

3 Answers  


Categories