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 happens if we override only equals?
What are inbuilt functions?
How many types of the indexof method are there for strings?
How many bits is a double?
What is arraylist e in java?
What is externalizable?
Write a java program to count the number of words present in a string?
What is multi-catch block in java?
Can inner class extend any class?
Differentiate between a constructor and a method? Can we mark constructors final?
What is a jagged array in java?
Which data type is class in java?
State one difference between a template class and class template.
What is mysql driver class name?
Can a class be final?