Question

I have a small simulator that simulates foxes and rabbits... I am trying to make fox and rabbit classes to implement actor class, but I get this error message, and i dont know whats wrong..

Error message: rabbit Is not abstract and does not override abstract method act (java.until.List) in Actor

Rabbit class

import java.util.List;
import java.util.Random;


public class Rabbit extends Animal implements Actor
{
    // Characteristics shared by all rabbits (static fields).

    // The age at which a rabbit can start to breed.
    private static final int BREEDING_AGE = 5;
    // The age to which a rabbit can live.
    private static final int MAX_AGE = 40;
    // The likelihood of a rabbit breeding.
    private static final double BREEDING_PROBABILITY = 0.15;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = 4;

    /**
     * Create a new rabbit. A rabbit may be created with age
     * zero (a new born) or with a random age.
     * 
     * @param randomAge If true, the rabbit will have a random age.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Rabbit(boolean randomAge, Field field, Location location)
    {
        super(field, location);
        if(randomAge) {
            setAge(rand.nextInt(MAX_AGE));
        }
    }

    /**
     * This is what the rabbit does most of the time - it runs 
     * around. Sometimes it will breed or die of old age.
     * @param newRabbits A list to add newly born rabbits to.
     */
    public void act(List<Actor> newActors)
    {
        incrementAge();
        if(isActive()) {
            giveBirth(newRabbits);            
            // Try to move into a free location.
            Location newLocation = getField().freeAdjacentLocation(getLocation());
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }


    public Animal getNewAnimal(boolean status, Field field, Location loc)
    {
        return new Rabbit(status, field, loc);
    }

    /**
     * Return the maximal age of the rabbit.
     * @return The maximal age of the rabbit.
     */
    protected int getMaxAge()
    {
        return MAX_AGE;
    }

    /**
     * Return the breeding age of the rabbit.
     * @return The breeding age of the rabbit.
     */
    protected int getBreedingAge()
    {
        return BREEDING_AGE;
    }

    /**
     * Return the breeding probability of the rabbit.
     * @return The breeding probability of the rabbit.
     */

    protected double getBreedingProbability()
    {
        return BREEDING_PROBABILITY;
    }

    /**
     * Return the maximal litter size of the rabbit.
     * @return The maximal litter size of the rabbit.
     */

    protected int getMaxLitterSize()
    {
        return MAX_LITTER_SIZE;
    }

}

Actor class

import java.util.List;
/**
 * Write a description of interface Actor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public interface Actor
{
    /**
     * performs actor's regular behaviour
     */
    void act(List<Actor> newActors);
    /**
     * is the actor still active
     */
    boolean isActive();
}
Was it helpful?

Solution

By implementing Actor, you promise to implement void act(List<Actor> xxx) method, but you did not. You did implement a method void act(List<Animal> xx), but this is a different method (has a different signature).

Simply, you are not correctly overriding act method:

public void act(List<Animal> newRabbits) != void act(List<Actor> newActors);

The signature must correspond. Since act is abstract in Actor (being an interface) you must override it in a class that implements Actor or declare it abstract (without being able to instantiate it.

OTHER TIPS

Interfaces are a contract that you will provide certain functionality in the classes that implements those interfaces. You MUST implement the act() method from the Actor interface if you are trying to create a concrete class.

You can read more here: http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

@Jack is right.

I would like to explain why this is not allowed. (Would be allowed if it was an Array. )

Consider Animal class

public class Animal{
      String name;

     //getters and setters
}

public class Dog extends Animal{

 public void woof(){  }
}

public class Cat extends Animal{
  public void meow(){}
}

PS: Following is NOT allowed but showing just to explain why it isnt allowed

 List<Animal> lst = new ArrayList<Animal>();

 lst.add(new Dog());
 lst.add(new Cat());

 makeNoise(lst); //**** IS NOT ALLOWED BUT SIMULATES YOUR SITUATION.  *****//

You have a method in some other class which is

 public makeNoise(List<Dog> dogNoises){ 

//iterate over the list and do follwoing for each Dog
//Remeber we put a cat in Dog list? Cat doesnt woof!!! it will break or crash or    something else you name it. 

for(Dog doggie: dogNoises)
    doggie.woof();   
}

Generic collections make sure we dont run into issues like above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top