java - Extending and returning values over classes -
i having trouble getting values 2 sub-class. how return values main program 2 other class? classes have hierarchy , cat.java extends animal.java. able values animal class not extended cat class. doing wrong?
main program
import java.util.*; public class animalproject { private static scanner input = new scanner(system.in); public static void main(string[] args) { system.out.println("welcome create own animal."); system.out.println("start typing name animal: "); string name = input.next(); animal newanimal = new animal(name, 0); system.out.println("new animal created"); system.out.println("set state of animal: [1] alive. [2] dead. "); int status = input.nextint(); newanimal.animalstate(status); system.out.println("print name of animal? [1] yes [2] no "); int answer = input.nextint(); if (answer == 1) { newanimal.getname(); } system.out.println("check status of animal? [1] yes [2] no"); answer = input.nextint(); if (answer == 1) { newanimal.checkstate(); } system.out.println("set lifes cat: "); int life = input.nextint(); // set lifes lifesbefore(). in cat.java system.out.println("remove lifes cat?: [1] yes [2] no"); while (true) { life = input.nextint(); { // call method decrease lifes cat.java } if (life == 2){ break; } } system.out.println("check cats lifes? [1] yes [2] no"); answer = input.nextint(); if (answer == 1) { // return lifes cat.java }
animal.java
public class animal{ protected string name; protected int status; public animal(string animalname, int animalstatus){ name = amimalname; state = animalstatus; } public void getname() { system.out.println(name); } public void setname() { this.name = name; } public void animalstate(int status) { if (status == 1) { state = 1; // dead } else if (status == 2) { state = 2; // alive } else { system.out.println("error setting state.. program closing.."); system.exit(1); } } public void checkstate() { if (state == 1) { system.out.println("animal dead "); } else if (state == 2) { system.out.println("animal alive"); } else { system.out.println("unkown input.. program closing.."); system.exit(1); } } }
cat.java
public class cat extends animal { private int catlifes; public cat(string animalname int animalstatus, int lifes) { super(animalname, animalstatus); catlifes = lifes; } public void lifesbefore(){ this.catlifes = lifes; } public void decreaselifes() { (int = 0 ; < catlifes; i++) { catlifes--; } system.out.println("cat ran out of lifes , dead! "); // set animals state dead in animal.java } public int catslifesafter(){ return this.catlifes; } }
cat extends animal
means every cat
is animal
not every animal
cat
.
so in code did create animal using new animal(name, 0)
, got animal
-object, not cat
- walrus.
but using animal newanimal = new cat(name, 0,9)
not able access cat
-methods. animal
created cat
, right, compiler knows newanimal
told when using animal newanimal
- animal
.
so might either using cat
beginning:
cat newanimal = new cat(name, 0,9);
but losing of features of inheritance want explore. alternative use type-check , cast later on:
if(newanimal instanceof cat) { cat acat = (cat)newanimal; /*...*/ acat.lifesbefore() /*...*/ }
Comments
Post a Comment