Storing objects in an array for retirevel and modification of instantiated objects (Java) -
hello new java , taking course of study in college studies. assignment doing polymorphism, , suppose create 4 objects: 2 without arguments, , 2 arguments.
personlocation personlocation = new personlocation(); persondata persondata = new persondata(); personlocation personlocationoverloaded = new personlocation("hamilton"); persondata persondataoverloaded = new persondata("frizzle", "289-549-1045");
now sure did properly, next part confused. asks "store objects in array retrieval , modification of instantiated objects". not sure how go this. thinking:
personlocation[] personlocation = new personlocation[4]; persondata[] persondata = new persondata[4];
but doesn't create sense because have 8 objects. thought of putting them together:
string[] employees = new string[4]; for(int = 0; < 2; i++) { employees[i] = personlocation; } for(int = 2; < 4; i++) { employees[i] = persondata; }
this seems create more sense because have superclass of location
, subclass of data
.
any pointers in right direction creating array of objects retrieval , modification of instantiated objects of great help.
if instructor looking have of objects in same array, need rely on fact objects in java extend super object called "object". so, in case, can create new array of type "object", in can mix , match objects set in (putting both info , locations in it). then, when looping through array, can utilize java keyword "instanceof", check if either personlocation or persondata. illustration wrote shown here:
public static void main (string args[]) { personlocation pl1 = new personlocation(); personlocation pl2 = new personlocation(); persondata pd1 = new persondata(); persondata pd2 = new persondata(); object[] array = new object[4]; array[0] = pl1; array[1] = pd1; array[2] = pl2; array[3] = pd2; for(object o : array){ if(o instanceof personlocation){ personlocation temppersonlocation = (personlocation) o; system.out.println(temppersonlocation); } if(o instanceof persondata){ persondata temppersondata = (persondata) o; system.out.println(temppersondata); } } }
which gave me output:
this personlocation persondata personlocation persondata
if have mutual interface or super class (such persondata inherits person or persondata extends person), utilize super class create single array such first illustration well. alter type of array "object" whatever interface/super class is.
it worth noting, in illustration create 2 arrays (one each object type), there no need instantiate size of 4 in each array, since have 2 of each type of objects. instead can instantiate size 2, , result in 4 objects total, not 8 had mentioned. illustration of is:
public static void main(string args[]) { personlocation pl1 = new personlocation(); personlocation pl2 = new personlocation(); persondata pd1 = new persondata(); persondata pd2 = new persondata(); personlocation[] personlocation = new personlocation[2]; persondata[] persondata = new persondata[2]; personlocation[0] = pl1; personlocation[1] = pl2; persondata[0] = pd1; persondata[1] = pd2; (personlocation pl : personlocation) { system.out.println(pl); } (persondata pd : persondata) { system.out.println(pd); } }
resulting in output of:
this personlocation personlocation persondata persondata
my guess if instructor has not taught super classes or instanceof keyword, looking sec illustration have showed.
java arrays object polymorphism
No comments:
Post a Comment