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

What is the purpose of stub and skeleton?

546


Define the term string pool?

582


What is singleton service?

508


Can we have return statement in finally clause? What will happen?

510


How can I become a good programmer?

486






How large is a boolean?

553


What is the integer of 16?

537


Which package is always imported by default?

543


Why do we need data serialization?

541


What happens to a static var that is defined within a method of a class?

549


Why isn’t there operator overloading?

592


What are the types of sockets in java?

540


What is thread life cycle in java?

578


Can you call a method in a method?

527


Can a abstract class be declared final?

563