The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

call subclass method from a superclass

Posted by Erik Buitenhuis on July 04, 2000 at 6:37 AM

From inside a superclass method I want to call another method that exists in that superclass, but is overridden by some of it's subclasses. see below:

class super {
void m1 (super s) {
// call method m2 of the (sub)class that s is an instance of
// the line below has invalid syntax but shows what I want:
// typecast s to s.getClass() and call the subclass's m2.
((s.getClass())s).m2();
}
void m2 () { ... }
}

class sub1 {
void m2 () { ... }
}

class sub2 {
void m2 () { ... }
}

All this without the superclass having to know what actual subclasses do exist (so I don't want to "switch ((s.getClass())"
over possible subclasses).

Anybody know how to do this????
-Erik





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us