Can we override static methods?

Answer Posted / sadheez

It may seems to be overriding the static methods, but the
real fact is HIDING.

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.

Here if u say it to be overriding then the
subclass ie., Bar class having static classMethod() should
be executed. But the fact here is Foo class static
classMethod() is executed.

So its method HIDING and not method
overriding..

I hope i have given answer to my best if
anyone feels wrong plz do post ur suggestions..

Is This Answer Correct ?    59 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an argument java?

523


What is the symbol for line break?

592


Can we catch more than one exception in a single catch block?

615


What's the default access specifier for variables and methods of a class?

497


what do you understand by synchronization? Or what is synchronization and why is it important? Or describe synchronization in respect to multithreading? Or what is synchronization? : Java thread

473






Can a abstract class be declared final?

547


What is unicode in java?

492


What is arraylist class in java?

497


Why deletion in linkedlist is fast than arraylist?

525


What does three dots mean in java?

637


How does class forname work in java?

468


Is alive and join method in java?

522


Which types of exceptions are caught at compile time?

552


What is public static void main?

556


What is the do while loop syntax?

544