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


Please Help Members By Posting Answers For Below Questions

Can we able to pass objects as an arguments in java?

527


What is protected access modifier?

565


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; }

1841


Can I override protected method in java?

587


What is indexof?

535






What are internal variables?

543


What's the base class in java from which all classes are derived?

731


Explain where variables are created in memory?

573


Implement a stack with push (), pop() and min() in O(1) time.

620


What is unicode in java?

496


What is a local, member and a class variable?

568


State differences between C and Java?

651


What do you mean by stream pipelining in java 8?

728


How are variables stored in memory?

568


What is a method vs function?

545