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 able to pass objects as an arguments in java?
What is protected access modifier?
public class Test { public static void main(String[] args) { int countA = 0; int countB = 0; int countC = 0; int countD = 0; int countE = 0; int countF = 0; int countG = 0; int countH = 0; int countI = 0; int countJ = 0; int countK = 0; int countL = 0; int countM = 0; int countN = 0; int countO = 0; int countP = 0; int countQ = 0; int countR = 0; int countS = 0; int countT = 0; int countU = 0; int countV = 0; int countW = 0; int countX = 0; int countY = 0; int countZ = 0; } } Can anybody tell me any alternate solution(like loop or something) to automate this initialization process. Ex:- for(char chr='A';chr<='Z'; chr++) { (int) String "count"+chr = 0; }
Can I override protected method in java?
What is indexof?
What are internal variables?
What's the base class in java from which all classes are derived?
Explain where variables are created in memory?
Implement a stack with push (), pop() and min() in O(1) time.
What is unicode in java?
What is a local, member and a class variable?
State differences between C and Java?
What do you mean by stream pipelining in java 8?
How are variables stored in memory?
What is a method vs function?