Hello all, I am a beginner at Java and taking a class which is moving at a fast pace.
So I have this Animal class and Fox class that must access the AnimalColor class through an API. Originally the AnimalColor object in the Animal class was public(accessible by all) but I now must change that to Private and force the Fox and Animal class to access it through an API. I am new to Java and having trouble locating info on API's used in this format. Here is the code for Animal:

import java.util.Random;  
public abstract class Animal{

     * myColor is protected so that subclasses can access them directly.
     * Not very good programming practice (but helps to illustrate private)
     * Better would be, like location, to have API methods to handle color.
    private Point location;
    private AnimalColor myColor;    //Changed from public to private

     * The constructor sets up the animal at a random location
        Random ranGen = new Random();
        location = new Point(10 + ranGen.nextInt(380), 10 + ranGen.nextInt(360));

     * The API allows the animal's location to be altered. This uses the Point API.
    public void setLocationX(int x){

    public void setLocationY(int y){

    public int getLocationX(){
        return location.getX();

    public int getLocationY(){
        return location.getY();

    public AnimalColor getColor(){
        return myColor;

     * Abstract method
    public abstract void move();

Here is the code for Fox:

public class Fox extends Animal{
     * All the constructor does is to set the color in which the fox should be
     * displayed.
        myColor = AnimalColor.ORANGE;

     * Right now, foxes don't know how to move. This is not an abstract method because
     * it has a (null) body
    public void move(){


Finally the AnimalColor class which needs to be accessed by Fox and Animal through an API:

public enum AnimalColor {BLACK, WHITE, ORANGE};

I will be using this (API's) a great deal in this class and would love to be able to not only code it, but understand how to apply it. I know the reason for an API, but simply cannot apply it.

Thank you in advance for all your advice and help.

Recommended Answers

All 13 Replies

Can you explain what your problem is?
Are you getting compiler errors?

Since I changed the object private AnimalColor myColor; from protected to private, my other functions cannot access it. I get the error "The field Animal.myColor is not
" I am supposed to use an API to access the AnimalColor enum function. Here are the exact instructions:

The current version of Animal makes myColor protected so that subclasses can access it. This is bad programming style.
Improve it by making myColor private
This will involve writing an API for myColor and retrotting it into the code, so that Animal and Fox use
the API

Add some getter and settor methods to the class with private field.

use an API to access the AnimalColor enum

That's misleading. What you are trying to do is access the myColor variable.

So then I will place them in the AnimalColor class right? And then access them through the Animal and Fox class?

The AnimalColor class defines the values available for an AnimalColor variable. There are no variables in that class.
The variable you want to set and get is in the Animal class. That is where the methods should go.

Is this the proper coding for set and get?

public void setColor(String c){
        this.c = myColor;

    public String getColor(){
        return c();

Aren't the methods supposed to allow access to the myColor variable?
It is not a String. Look at the Animal class and see what type it is.
Also look at the method defined at line 40 of the Animal class.

Makes sense it is not a string, it is of type AnimalColor. Does this look right?

public void setColor(AnimalColor c){
        this.c = myColor;
    public AnimalColor getColor(){
        return myColor;

Does it compile? What happens when you test it?
Create an object for a class that extends the Animal class, call its set method and then call its get method and print out what is returned to see if it works.

Well I have not compiled it but Im not getting any errors from eclipse which is good. Can you post the coding and slight explanation of calling these set and get methods from the Fox class? I am at a loss with calling them from the Fox class. Fox extends Animal.

Since the methods are in the Animal class, any method in the Fox class or its constructor can call them directly.

For testing, add the calls to the two methods to the Fox class's constructor.
Add a main() method to the Fox class and have it create an instance of the Fox class by using the new statement. That will cause the constructor to be called and will execute the set and get methods in the constructor. When testing is over, the testing code can be removed.

So I finally firgured out how to call the method!!

    setColor (AnimalColor.ORANGE);

ALthough Im not sure if I even need a getColor in the Fox class. Since I am just setting the color is getColor() necessary?

For testing, You need to print out what the get method returns to be sure the get and set methods worked correctly.

Most get methods return a value. The posted code does not assign the returned value to any variable, so it's lost. Assign it to a variable and print out that variable.

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.