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 have a try block without catch block?

571


What is a flag value?

523


What is string in java?

568


What are annotations in java?

623


Describe the syntax of multiple inheritance? When do we use such an inheritance?

591






What is the properties class in java programming?

555


What data type is string java?

544


Can we have two main methods in a java class?

542


What is jit and its use?

591


How do you read a char in java?

467


Is there a way to increase the size of an array after its declaration?

606


How is treeset implemented in java?

498


What is %02d?

583


What are the important features of Java 9 release?

540


Is an object null?

564