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
Can you run java program without main method?
What is the use of predicate in java 8?
How many types of parsers are there?
What is a java applet? What is an interface?
What is a buffer in java?
What is an image buffer?
What does file separator do in java?
What are the special characters?
Can a main method be declared final?
What is used of static keyword in java?
Can a static class have a constructor?
Is void a data type?
Why java is a platform independent? Explain
What is OOP Language?
What do you understand by looping in java? Explain the different types of loops.