Java Reference
In-Depth Information
public class DuplicateObjects {
public static void main(String[] args) {
PetDog myPet = new PetDog("Fang", "Chihuahua", "Max", "Circus
flea");
System.out.println("\nMy pet details:\n"+ myPet);
PetDog yourPet = new PetDog(myPet);
System.out.println("\nYour pet details:\n"+yourPet);
yourPet.setName("Gnasher");
// Change your dog's
name
yourPet.getFlea().setName("Atlas");
// Change your dog's
flea's name
System.out.println("\nYour pet details:\n"+yourPet);
System.out.println("\nMy pet details:\n"+ myPet);
}
}
Directory "DuplicateObjects"
I put the files in a DuplicateObjects folder. You need to add the files containing the new versions of
the Dog and Animal classes.
This produces the following output:
My pet details:
This is a Dog
It's Fang the Chihuahua - a pet dog.
It has a flea:
This is a Flea
It's Max the circus flea
Your pet details:
This is a Dog
It's Fang the Chihuahua - a pet dog.
It has a flea:
This is a Flea
It's Max the circus flea
Your pet details:
This is a Dog
It's Gnasher the Chihuahua - a pet dog.
It has a flea:
This is a Flea
It's Atlas the circus flea
My pet details:
This is a Dog
It's Fang the Chihuahua - a pet dog.
It has a flea:
Search WWH ::




Custom Search