Posted by Mars on 2000-06-11
As far as I know there isn't any other method. But what is the problem of
calling first super.hello() and then execute some other code ? If you need
to redefine a method in a sub-class and have it execute the parent's code
you must use the super code.
Thinking about it, there may be another method. If you substitute class foo
with an abstract class (lets' say class C) then let class bar extend from C,
it can be done.
abstract class C {
void hello()
System.out.println("Hello");
myOwnCode();
}
asbtract void myOwnCode();
}
All you have to do is then for class bar, put the extra code in the
myOwnCode() function i.e.
public class bar extends C {
void myOwnCode() {
System.out.println("Additional code");
}
}
Note that the function hello is inherited automatically by bar.
Additionally whenever you call hello from bar, first it executes the parent
code (i.e. the code of class C), and then it calls myOwnCode which is
implemented in bar.
Basically the output for bar.hello() is
Hello
Additional code
If you need a better explanation, tell me.
Mars
Previous post | Next post | Timeline | Home