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

Can private method static?

466


What do you mean by ternary operator in java?

550


What is a copy constructor in java?

572


How to split arraylist elements in java?

582


What is the requirement of thread in java?

610






What is the use of flag?

575


How many tetrahedral voids are there in bcc?

536


How would overload a function based on return type?

542


When should you use arraylist and when should you use linkedlist?

484


How do you trim a space in java?

496


What language is an assembler written in?

539


Is set sorted in java?

508


Does java map allow duplicates?

483


What is arrays aslist in java?

505


what is comparable and comparator interface?

567