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

Popular posts from this blog

javascript - IndexedDB error: Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned -

amazon web services - When using Cognito credentials with AWS in a browser (javascript), keep getting "missing credentials" error -

c# - How to set the datatextfield value instead of the value on a drop down list? -