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


Please Help Members By Posting Answers For Below Questions

What is synchronization and why is it important?

617


Will the general public have access to the infobus apis?

623


Whats new with the stop(), suspend() and resume() methods in jdk 1.2?

674


What restrictions are placed on the location of a package statement within a source code file?

718


What is the relationship between an event-listener interface and an event-adapter class?

643






Is the infobus client side only?

735


What is a tasks priority and how is it used in scheduling?

640


What are the types of scaling?

658


Do I have to use jsps with my application?

662


which type of objects reference will be given to client?

2135


What is the purpose of the finally clause of a try-catch-finally statement?

659


What are various types of class loaders used by jvm?

594


What is scalable, portability in the view of J2EE?

1988


Why use a datasource when you can directly specify a connection details?

616


If your ui seems to freeze periodically, what might be a likely reason?

654