java - Call a subclass method from superclass which is not available in another sub class -
i have superclass person
, 2 subclasses author
, member
follows
public class author extends person { public author(string fname, string lname, int noofpublications){ ----// set } public string getnoofpub(){ return noofpublications; } } public class member extends person { public member(string fname, string lname, int memberno){ ----// set } public string getmembno(){ return memberno; } }
i want access non common methods main class or how can access getnoofpub , getmembno methods superclass person?
you can gain access non-common methods checking type of object , casting type of relevant sub-class, that's not you'd want in code of base class.
in method of person, can write :
public void somemethod () { string id = null; if (this instanceof member) { member member = (member) this; id = member.getmembno (); } }
while valid syntax, it's bad practice, since base class shouldn't know sub-classes, since sub-classes can written long after base-class written, writers of base-class can't assume know sub-classes extend base-class.
Comments
Post a Comment