Chapter 7: Pet Shop v2 – Inheritance and Interface

Pet Shop v2 Application Project

PetShop v2

Without changing the Animal class add a new instance field, name. Each animal class ( like Bird ) implements Nameable.

public interface Nameable
{
    String getName();
}

PetSet2
This class groups the objects of different classes, different animal classes, that are only associated by the Nameable interface type. Write the class PetSet2 to add objects of the Nameable interface type.

PetShop2
This class tests the PetSet2 class by adding Nameable type objects and printing the PetSet2 collection.

Sample test:

…
PetSet2 myPetShop2 = new PetSet2();
        
Nameable tweety = new Bird2("tweety",45.00);
Nameable duffy = new Bird2("duffy",5.00);
Nameable nemo = new Fish2("nemo",62.00);
Nameable bear = new Dog2("bear",1100.00);
…        
…
myPetShop2.add(tweety);
myPetShop2.add(duffy);
myPetShop2.add(nemo);
myPetShop2.add(bear);
…
System.out.println(myPetShop2);

Sample output:

My name is tweety
My name is duffy
My name is nemo
My name is bear

….

I can sing

….

NOTE: Again, how close you follow the outline and polymorphism concepts will determine your grade