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

How finally used under exception handling?

718


What is skeleton and stub?

760


Can we increase size of array?

761


What one should take care of, while serializing the object?

673


What is the format specifier?

710


What is a java lambda expression?

787


what is anonymous class in java?

757


What is default switch case? Give example.

784


Why java is used everywhere?

758


What is meant by null and void?

733


What is an argument in java?

699


How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?

764


Explain numeric promotion?

811


Wha is the output from system.out.println(“hello”+null); ?

895


How a variable is stored in memory?

725