Kendall Ponder wrote:Is there a way to do it directly from an instance variable of the subclass?
No, you can't!
As already mentioned by Douglas, you can use super()
only to invoke constructor from the superclass and this()
to invoke an overloaded constructor as shown in the following code snippet:
Kendall Ponder wrote:I haven't seen anything like this so I assume I would have to write a new method in SubClass to call the SuperClass method.
That's indeed a possibility. But you have to be careful and invoke this method using the super
keyword, like Jeanne has suggested. Otherwise you still would invoke the method()
method in SubClass
. Illustrated with this code snippet:
Another possibility would be to refactor the SuperClass
class a little bit: you define an additional final
method (subclasses can't override this method) which has the println-statement; the current method()
method simply invokes this final
method. As shown in this code snippet:
Hope it helps!