java - Invoke a method on all subclasses from superclass? -



java - Invoke a method on all subclasses from superclass? -

i have stubled upon has annoyed me.

whenever want method invoked in classes have interface, or if extensions, have keyword opposite of keyword super. basically, want invocation passed downwards (if class inherits method, , method in superclass called, called in subclass well). there resembles asking for?

edit: contemporary methods using efficient, not efficient them be. wondering if there way of invoking method, has been inherited, superclass/superinterface. lastly time looking this, did not find either.

note: of subclasses unknown, hence impossible utilize. known class superclass, why can't invoke it. can solved using reflections api, using. however, not comply searching for.

every method in java virtual exception of static methods, final methods , constructors meaning if subclass implements method beingness invoked, subclass's implementation called. if subclass wishes invoke immediate superclass method, accomplished via phone call super.

this mutual abstract classes base of operations class utilized framework, clients expected override. instance:

public abstract class drawer{ public void draw(){ //setup code, etc mutual subclass implementations dodraw(); } protected abstract void dodraw(); } public class circledrawer extends drawer{ protected void dodraw(){ //implementation of how draw circle } }

now, when have instance of circledrawer , phone call draw(), superclass drawer.draw() method invoked is, in turn, able phone call cicledrawer.dodraw().

edit now, if circledrawer this:

public class circledrawer extends drawer{ public void draw(){ //do stuff } protected void dodraw(){ //implementation of how draw circle } }

any invocation of drawer.draw() on instance of circledrawer always invoke circledrawer.draw() method.

java inheritance super

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -