java - How to use polymorphism in ArrayLists? -


so have zoo program want have list of rooms in zoo, , list of cats in each room.

i have 3 classes: felid, housecat , wildcat - housecat , wildcat extend felid. depending on literal class of animal (right have tiger, persian, siamese , cheetah - tiger , cheetah extend wildcat, persian , siamese extend housecat) attributes automatically assigned.

class diagram - http://i.imgur.com/vtsnrva.jpg

for example these fields felid:

string speciesname; string furcolour; string gender; int weightpounds; boolean packanimal; string habitat; int age; 

these fields housecat:

string ownername; string catname; boolean feral; 

these fields wildcat:

boolean maneater; 

in constructor housecat have

    if(catname == null || catname.equals("")){         feral = true;     }  

and if cat's feral, when user creates housecat cat name , uses 'printcatinfo()':

@override public void printcatinfo(){     if(feral){         system.out.println("feral" + "\n" + speciesname + "\n" + furcolour + "\n" + gender +              "\n" + weightpounds + "lbs\n" + "is not pack animal" + "\n" + habitat + "\n" + age + " years old (human)");     }     else{         system.out.println("owner name is: " + ownername + "\n" + "cat name is: " + catname + "\n" + speciesname + "\n" + furcolour + "\n" + gender +              "\n" + weightpounds + "lbs\n" + "is not pack animal" + "\n" + habitat + "\n" + age + " years old (human)" + "\n");     }      } 

it won't print name.

feral siamese white or grey abdomen black legs, face , tail male 8lbs not pack animal urban 7 years old (human) 

the trouble counted cats feral assumed because list used list of cats is:

arraylist<felid> catlist = new arraylist<felid>(); 

so guess catname null because housecats added list count type 'felid'.

how create list can throw of cats , still treat them respective classes?

edit: pointing out assignment operator error, it's still printing feral though

final edit: thank 'doubledouble' pointing out how use 'super()' me - that's not expected problem was. problem:

public class siamese extends housecat{     public siamese(int weightpounds, int age, string ownername, string catname, string gender){         this.speciesname = "siamese";         this.furcolour = "white or grey abdomen black legs, face , tail";                 this.ownername = ownername;         this.catname = catname;         this.weightpounds = weightpounds;         this.age = age;         this.gender = gender;     } } 

new code:

public class siamese extends housecat{     public siamese(int weightpounds, int age, string ownername, string catname, string gender){         super(catname);         this.speciesname = "siamese";         this.furcolour = "white or grey abdomen black legs, face , tail";                 this.ownername = ownername;         this.catname = catname;         this.weightpounds = weightpounds;         this.age = age;         this.gender = gender;     } } 

your constructors seem following (correct me if wrong):

public siamese(int weightpounds, int age, string ownername, string catname, string gender) {     this.weightpounds = weightpounds;     this.age = age;     this.ownername = ownername;     this.catname = catname;     this.gender = gender; }  public housecat() {     if(catname == null || catname.equals(""))     {         feral = true;     } } 

since ownername, catname, , feral part of housecat class, best let housecat constructor handle fields.

public housecat(string ownername, string catname) {     this.ownername = ownername;     this.catname = catname;     if(catname == null || catname.equals(""))     {         feral = true;     } } 

siamese looks then:

public siamese(int weightpounds, int age, string ownername, string catname, string gender) {     super(ownername, catname);     this.weightpounds = weightpounds;     this.age = age;     this.gender = gender; } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -