java - Child Instantiate from parent class -


it possiable instantiate child object parent class without child class name.

for example, have next classes:

public class {    protected int a;     public a1() {        = 0;    }     public int geta() {        return a;    }     public static getobject() {        // code need write    } }  public class a1 extends {     public a1() {         = 5;     } }  public class a2 extends {     public a2() {         = 10;     } } 

usage example:

a = a1.getobject(); a.geta(); // return 5  = a2.getobject(); a.geta(); // return 10  = a.getobject(); a.geta(); // return 0 

a1, a2 not child classes. there may unlimited numbers.

how can write getobject() method creates class childs instanses.

ps:

i need initialize child class object, there large amounts of child classes , not call "new" of them , not write static method initialize it, also, childs have same constructors. big lack can't create child instance parent.

when write a = a1.getobject(), use child classname (a1). a) question misleading , b) why can't write a = new a1()?

if want write a1.getobject(), can redefine getobject() in class a1:

public class a1 extends {    public static getobject() {      return new a1();    }  } 

without redefining, there no way declare getobject() in class return objects different classes because a = a1.getobject() compile a = a.getobject().


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -