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 does module-relative mean?
Where can I ask questions and make suggestions about seam?
Which containers use a border layout as their default layout?
What is the difference between long.class and long.type?
What is the diffrence between a local-tx-datasource and a xa-datasource?
What is a modular application? What does module-relative mean?
Why are some of the class and element names counter-intuitive?
Describe responsibilities of Activator?
Difference between new operator and class.forname().newinstance()?
Define aop(assepct oriented programing)?
Name the class that is used to bind the server object with RMI Registry?
What are the oops concept?
what is the use of State Factories?
To identify IDL language what mapping mechanism is used?
int x=5,i=1,y=0; while(i<=5) { y=x++ + ++x; i=i+2; } System.out.println(x); System.out.println(y); System.out.println(i); How to solve this? Please explain!