package inheritAnimals; // superclass has variables and methods shared by all subclasses // doesn't say it extends anything, so inherits from Object public class Pet { // variables all Pets need // protected means available in subclasses // but also available in rest of package -- beware!! protected String name; protected int age; // constructors are NOT inherited // can be called from subclass constructors with super() public Pet() { setName("animal"); setAge(1); } // can be called from subclass constructors with super(name, age) public Pet(String name, int age) { this(); setName(name); setAge(age); } // behaviors all Pets do public void eat() { System.out.println(getName() + " eats a food."); } public void sleep() { System.out.println(getName() + " sleeps. honk-shu, hong-shu."); } public void makeNoise() { System.out.println(getName() + " goes yip"); } // standard methods all Pets have public String getName() { return name; } public void setName(String nameval) { if (nameval != null) { this.name = nameval; } } public int getAge() { return age; } public void setAge(int ageval) { if (ageval >= 0) { this.age = ageval; } } @Override // since Object has toString, we are overriding public String toString() { return getName() + "(" + getAge() + " years)"; // could choose to also see garbage given by Object's toString //return getName() + "(" + getAge() + " years) [" + super.toString() + "]"; } }