Hi

My issue is i cant get the arrys correct,with abstract class.Can some one highlight to me the error in class Example1.I want to create 2 extra pets (iguana & hamster) but i am getting it wrong.

below are the errors from my complier

23.java:37: error: constructor GoldFish in class GoldFish cannot be applied to given types;
owner.pets[2]=new GoldFish();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
23.java:38: error: constructor Dog in class Dog cannot be applied to given types;
owner.pets[3]=new Dog("Hamster");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
23.java:39: error: constructor Dog in class Dog cannot be applied to given types;
owner.pets[4]=new Dog("Iguana");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
3 errors

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines


abstract class Pet {

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(String action){
this.action = action;
}
public String getAction(){
return 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 Example1 {
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 Dog("Hamster");
    owner.pets[4]=new Dog("Iguana");

    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();
            }
        }
    }

}

}

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.