can a static method be overridden
Answer Posted / raghvendra
Look at the code and resule below and Interpret it
yourself .. :) .. it is pretty easy.
public class A {
public static void staticMethod(){
System.out.println("Static: I print from
A");
}
public void nonStaticMethod(){
System.out.println("Non Static: I print
from A");
}
}
public class B extends A{
public static void staticMethod(){
System.out.println("Static: I print from
B");
}
public void nonStaticMethod(){
System.out.println("Non Static: I print
from B");
}
}
public class Launcher {
public static void main(String[] args) {
A a = new A();
B b = new B();
A obj = new B();
System.out.println("obj instanceof A : " +
(obj instanceof A));
System.out.println("obj instanceof B : " +
(obj instanceof B));
a.staticMethod();
a.nonStaticMethod();
b.staticMethod();
b.nonStaticMethod();
obj.staticMethod();
obj.nonStaticMethod();
}
}
Consol Output:
obj instanceof A : true
obj instanceof B : true
Static: I print from A
Non Static: I print from A
Static: I print from B
Non Static: I print from B
Static: I print from A <--- See the difference here.
Non Static: I print from B <--- See the difference here.
Good luck!
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What do you mean by Socket Programming?
What is the relationship between an event-listener interface and an event-adapter class?
Explain the difference between object state and behavior?
How are commas used in the intialization and iteration parts of a for statement?
how do you Handle Front End Application data against DB with example?
If I wanted to use a solarisui for just a jtabbedpane, and the metal ui for everything else, how would I do that?
For which statements does it make sense to use a label?
Where can I ask questions and make suggestions about seam?
which book is better for jdbc ,servlets and jsp
How task's priority is used in scheduling?
What is the difference between the session.update() method and the session.lock() method?
What are the different class loaders used by jvm?
What are the different approaches to represent an inheritance hierarchy?
Why does the option tag render selected=selected instead of just selected?
What is the immediate superclass of the applet class?