Can we override static methods?

Answer Posted / neha jain

dear yogesh
sory for wrong answer we can not override static method
with static method that known as method hiding.plz view
following example
Briefly, when you override a method, you still get the
benefits of run-time polymorphism, and when you hide, you
don't. So what does that mean? Take a look at this code:

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 Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
If you run this, the output is
instanceMethod() in Bar
classMethod() in Foo

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is Java a dying language?

569


What is final, finally, finalize?

596


According to java operator precedence, which operator is considered to be with highest precedence?

601


Do we need to manually write Copy Constructor?

604


Is heap stored in ram?

524






When is the finalize() called?

696


What is anagram in java?

516


Write a function for palindrome and factorial and explain?

656


What is a class reference?

550


What is meant by overloading?

586


Can memory leak in java?

585


Is list thread safe in java?

489


What Is Composition?

579


Can you give few examples of final classes defined in java api?

559


Is string a class?

554