My only issue here is i am not able to invoke line 41
owner.pets[ 3 ].getAction();?
Any ideas why?
Inline Code Example Here
abstract class Pet1 {
abstract void perform();
}
class Dog extends Pet {
void perform() { System.out.println("*The dog rolls over*"); }
}
class GoldFish extends Pet{
void perform() {
System.out.println("*GoldFish eats alot*"); }
public String action = "Blowing bubbles";
GoldFish(){
}
GoldFish( String action ){
this.action = action;
}
public String getAction(){
return this.action;
}
}
class Human {
String firstName = "Unknown";
String lastName = "Unknown";
Pet[] pets = new Pet[ 5 ];
void speak() { System.out.println( "My name is " + firstName + " " + lastName ); }
}
class Example2 {
public static void main( String[] args ) {
Human owner = new Human();
owner.firstName = "Andy";
owner.lastName = "Cole";
owner.speak();
owner.pets[ 0 ] = new Dog();
owner.pets[ 1 ] = new Fish();
owner.pets[ 2 ] = new GoldFish();
owner.pets[ 3 ] = new GoldFish("Hamster");
owner.pets[ 4 ] = new GoldFish("Iguana");
owner.pets[ 3 ].getAction();
System.out.println("My pets will perform the following tricks:");
if ( owner.pets != null ) {
for ( int i = 0; i < owner.pets.length; i++ ) {
Pet somePet = owner.pets[i];
if ( somePet != null ) {
somePet.perform();
}
}
}
}
}