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 is data type modifier?
What is args length in java?
what invokes a threads run() method? : Java thread
Why is singleton instance static?
When parseint method can be used?
Can a singleton class be inherited?
Why is an interface be able to extend more than one interface but a class can’t extend more than one class?
Which is faster call by value or call by reference?
Say any two properties in beans?
What is the base class of all exception classes in java?
explain copyonwritearraylist and when do we use copyonwritearraylist?
What is a numeric format?
What is the purpose of the finally clause of a try-catch-finally statement in java programming?
can java object be locked down for exclusive use by a given thread? : Java thread
What is parameter example?