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 finally used under exception handling?
What is skeleton and stub?
Can we increase size of array?
What one should take care of, while serializing the object?
What is the format specifier?
What is a java lambda expression?
what is anonymous class in java?
What is default switch case? Give example.
Why java is used everywhere?
What is meant by null and void?
What is an argument in java?
How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?
Explain numeric promotion?
Wha is the output from system.out.println(“hello”+null); ?
How a variable is stored in memory?