Can we override static methods?
Answer Posted / 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 |
Post New Answer View All Answers
What is the purpose of stub and skeleton?
Define the term string pool?
What is singleton service?
Can we have return statement in finally clause? What will happen?
How can I become a good programmer?
How large is a boolean?
What is the integer of 16?
Which package is always imported by default?
Why do we need data serialization?
What happens to a static var that is defined within a method of a class?
Why isn’t there operator overloading?
What are the types of sockets in java?
What is thread life cycle in java?
Can you call a method in a method?
Can a abstract class be declared final?