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 to deploy tomcatserver to weblogic server? write d following steps?
What is the size of int in 64-bit jvm?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
Is nan false?
What is java lang string?
Does .length start 0 java?
What does java ide mean?
What is lambda in java?
What is a variable in java?
What is collection sort in java?
Which browsers work with java?
What are the Main functions of Java?
Does string is thread-safe in java?
What is __ init __ functions?
Define canvas?