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 is the difference between a static and a non-static inner class?
what is an isolation level?
What is a policy?
how do you Handle Front End Application data against DB with example?
Difference between new operator and class.forname().newinstance()?
What is ripple effect?
What is the difference between static and non-static with examples?
What is permgen or permanent generation?
What is the difference between java class and bean?
Define aop(assepct oriented programing)?
Is “abc” a primitive value?
Are enterprise beans allowed to use thread.sleep()?
When is the best time to validate input?
Can constructors be synchronized in java?
What are the benefits of detached objects?