You are on page 1of 3

______________________________________________

public abstract class Pet {


protected String name;
public Pet()
{
name = "unknown";
}
public Pet (String n)
{
name = n;
}
public String getName()
{
return name;
}
public String move()
{
return "run";
}
public abstract String speak();
public String toString()
{
return "My pet " + name;
}
}

_____________________________________________________

public class Dog extends Pet{


protected int weight;
protected String name;
public Dog(String s, int weight) {
super(s);
this.weight=weight;
}
public int getWeight()
{
return weight;
}

public String toString()


{
return super.toString()+" is a dog and weighs "+weight +" pounds";
}
public String move() {
return "run";
}

public String speak() {


return "woof woof";
}
}

________________________________________________________________

public class Snake extends Pet{


int length;
protected String name;
public Snake(String s, int len)
{
super(s);
name = s;
this.length = len;
}
public int getLength()
{
return length;
}
public String speak()
{
return "hiss hiss";
}
public String move()
{
return "Sligther";
}
public String toString()
{
return "My pet___ is snake and is _____ inches long";
}
}

__________________________________________________

public class poodle extends Dog{


public poodle(String n, int w)
{
super(n,w);
this.weight = w;
}
public String speak()
{
return "yip yip";
}
public String toString()
{
return "My pet _____ is a poodle and weight _____ pounds";
}
}

_________________________________________________________

public class pets2 {


public static void main(String args[]) {
Pet[] myPet = new Pet[4];
myPet[0] = new Dog("George",100);
myPet[1] = new Dog("Spot", 20);
myPet[2] = new poodle("Rocky", 16);
myPet[3] = new Snake("Bruno", 10);
for (int j = 0; j < myPet.length; j++) {
System.out.println(myPet[j].toString());
System.out.println("Speak: " + myPet[j].speak());
System.out.println(myPet[j].move() + " " + myPet[j].getName() + "\n");
if(myPet[j] instanceof Dog) {
//System.out.println(" Weight: " + myPets[j].getWeight());
System.out.println(" Weight: " + ((Dog)myPet[j]).getWeight());
}
}
}
}

You might also like