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 we have a try block without catch block?
What is a flag value?
What is string in java?
What are annotations in java?
Describe the syntax of multiple inheritance? When do we use such an inheritance?
What is the properties class in java programming?
What data type is string java?
Can we have two main methods in a java class?
What is jit and its use?
How do you read a char in java?
Is there a way to increase the size of an array after its declaration?
How is treeset implemented in java?
What is %02d?
What are the important features of Java 9 release?
Is an object null?