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
Explain exception chaining in java?
How objects of a class are created if no constructor is defined in the class?
Explain the use of javap tool.
Is class is a data type?
What happens when a thread cannot acquire a lock on an object in java programming?
What is java in detail?
What is the difference between an if statement and a switch statement?
State the main difference between c++ and java?
Why synchronization is important in java?
Is vector thread safe in java?
How do you reverse a string in java?
What is the significance of java packages?
When do we use synchronized methods in java?
What is java dot?
What is java util concurrentmodificationexception?