K
Ken Kafieh
[I'm reposting this because it didn't see to go up the first time. sorry if
this is a repeat]
Hi,
When you override a non-static method, a call to that method from the
super-class properly results in the sub-class version of that method being
called. BUT, when you override a static method, it appears --to my
frustration-- that a call to that method from the super-class results in the
super-class version of the method being called!
Here is an example of what I mean. My intention is for the following to
print out "Success" but it always prints out "Failure." instead.
It seems to ignore the fact that I have overrided method2( ) from ClassA
with method2( ) from ClassB.
class ClassA
{ static void method1 ( ) {
}
static void method2 ( ) {
System.out.println("Failure."); }
}
class ClassB extends ClassA
{ public static void main (String[] args) {
}
static void method2 ( ) {
System.out.println("Success."); }
}
I can get around the this problem by removing the static modifier, creating
a third class, and instantiating a ClassB object. Like below. But I'd
rather not have to create a third class, and instantiate an object if there
is a way to to keep the code smaller,simpler,easier. Is there a way to fix
the code above so that it prints "Success" instead? It just seems like
there ought to be!
class ClassA
{ void method1 ( ) { method2( ); }
void method2 ( ) { System.out.println("Failure."); }
}
class ClassB extends ClassA
{ void method2 ( ) { System.out.println("Success."); }
}
class Start
{ public static void main (String[] args)
{ ClassB b = new ClassB( );
b.Method1( );
}
}
-Ken
this is a repeat]
Hi,
When you override a non-static method, a call to that method from the
super-class properly results in the sub-class version of that method being
called. BUT, when you override a static method, it appears --to my
frustration-- that a call to that method from the super-class results in the
super-class version of the method being called!
Here is an example of what I mean. My intention is for the following to
print out "Success" but it always prints out "Failure." instead.
It seems to ignore the fact that I have overrided method2( ) from ClassA
with method2( ) from ClassB.
class ClassA
{ static void method1 ( ) {
}
static void method2 ( ) {
System.out.println("Failure."); }
}
class ClassB extends ClassA
{ public static void main (String[] args) {
}
static void method2 ( ) {
System.out.println("Success."); }
}
I can get around the this problem by removing the static modifier, creating
a third class, and instantiating a ClassB object. Like below. But I'd
rather not have to create a third class, and instantiate an object if there
is a way to to keep the code smaller,simpler,easier. Is there a way to fix
the code above so that it prints "Success" instead? It just seems like
there ought to be!
class ClassA
{ void method1 ( ) { method2( ); }
void method2 ( ) { System.out.println("Failure."); }
}
class ClassB extends ClassA
{ void method2 ( ) { System.out.println("Success."); }
}
class Start
{ public static void main (String[] args)
{ ClassB b = new ClassB( );
b.Method1( );
}
}
-Ken