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
How to print an arraylist in java?
What is the difference between compiler and jvm?
How to split a string in java?
Explain about core java?
What is the equal sign?
What is the main advantage of passing argument by reference?
Why stringbuffer is faster than string?
Explain runtime exceptions?
Can we catch more than one exception in single catch block?
Where pragma is used?
What is java jit compilers?
Is a string literal?
Write a method that will remove given character from the string?
Why are there no global variables in java?
What is internal iteration in java se 8?