Member Avatar for Amoryethel

Hi. I'm making an interface and a class that correlates with the client code below. Everytime I try to compile the client code, I get the following errors:

Bird.java:15: = expected
private Color col;
^
Bird.java:16: = expected
private Point pos;

As well as a few others. For now, I'm concerned about this. I know that it has something to do with my interface obviously. However everytime I tweak with it and try to make it similar to the Hummingbird class, the compile error gets worse. Thank you for your time.

Client Code

import java.awt.*;

public class Aviary
{
   public static final int SIZE = 20;
   public static final int PIXELS = 10;
   
   public static void main(String[] args)
   {
      DrawingPanel panel = new DrawingPanel(SIZE * PIXELS,
					                             SIZE * PIXELS);
      Graphics g = panel.getGraphics();
      
      Bird[] birds =
      {
         new Hummingbird(2, 9),	new Hummingbird(16, 11),
      };
      
      while (true)
      {
         g.setColor(Color.WHITE);
         g.fillRect(0, 0, SIZE * PIXELS, SIZE * PIXELS);

         for (Bird bird : birds)
         {
            bird.fly();
            g.setColor(bird.getColor());
            Point pos = bird.getPosition();
            g.fillOval(pos.getX() * PIXELS,
                       pos.getY() * PIXELS,
                       PIXELS, PIXELS);
         }

         panel.sleep(500);
      }
   }
}

Interface

public interface Bird
{
   public void fly();
	
   private Color col;
   private Point pos;
}

Hummingbird Class

import java.awt.*;

public class Hummingbird implements Bird
{	
	private Color col;
	private Point pos;
	
	public Hummingbird (int x, int y)
	{
		this.col = new Color(210, 10, 55);
		this.pos = new Point(x, y);
	}
	
	public Color getColor()
	{
		return col;
	}
	
	public Point getPosition()
	{
		return pos;
	}
	
	public void fly()
	{
		pos = new Point((int)(Math.random()* 19),(int)(Math.random() * 19));
	}
}

I don't think Bird should be an interface. It should be a class and should contain col and position and should have the functions getColor() and getPosition() and fly() as well as the getters and setters for col and position.

Hummingbird should EXTEND rather than IMPLEMENT Bird.

If you want use an interface, set up an interface called Flight containing the fly() function and have Bird implement Flight.

Bird should be an object, not an interface. A Hummingbird is a type of Bird, so it should extend Bird.

Member Avatar for Amoryethel
public class Bird
{	
   public void fly()
	{
	
	}
 
	public void Color()
	{

	}
	
	public void Point()
	{

	}
}

Like so?

public class Bird
{	
   public void fly()
	{
	
	}
 
	public void Color()
	{

	}
	
	public void Point()
	{

	}
}

Like so?

Yes on "fly" perhaps, but no on Color() and Point().

Take the code you have now in HummingBird and sick it in Bird.

private Color col;
   private Point pos;

But actually change "private" to "protected" here since if they're private and in Bird, HummingBird won't have access. Changing them to "protected" allows Hummingbird to have access.

So I think you are looking at this, with some very small changes, a verbatim copy and paste of Hummingbird into Bird, changing the word "Hummingbird" to "Bird" and making the "private" protected..

import java.awt.*;

public class Bird
{	
	protected Color col;
	protected Point pos;
	
	public Bird (int x, int y)
	{
		this.col = new Color(210, 10, 55);
		this.pos = new Point(x, y);
	}
	
	public Color getColor()
	{
		return col;
	}
	
	public Point getPosition()
	{
		return pos;
	}
	
	public void fly()
	{
		pos = new Point((int)(Math.random()* 19),(int)(Math.random() * 19));
	}
}

Hummingbird ends up being this:

public class Hummingbird extends Bird
{
    public Hummingbird(int x, int y)
    {
        super(x, y);
    }
}

You now have some base Bird activity and attributes. Anytime Hummingbirds act the same, it will use the Bird methods. Anytime Bummingbirds need special coding, stick it in the Hummingbird class so it will override th Bird methods. Anything common to all Birds goes in Bird. Anything specific to Hummingbird goes in Hummingbird.

- I agree that Bird is a class, not an interface. For the most part, interfaces define capabilities, classes define things and types of things. "Aviator" might be an interface, it would tell you that implementing classes have a method "fly()".

-Since you don't want to have a generic Bird wandering around in the world (you want Hummingbirds and Bluejays and like that, but not Birds) it should be an abstract class.

- color and position should be variables. You can make getters for them as well, but if every Bird has a color and a position, then you need a place to put them.


- in the class hierarchy, implement methods at the highest level possible. If they can't be implemented at the superclass level (because different inheriting classes execute the method in different ways, for example) then make the methods abstract at the top level.
so:

public Color getColor(){
  return color;
}
public abstract void call();  // coo, or squawk, or chirp, or crow, as appropriate

- Try to set things up so you can implement at the highest possible level, without reaching down for specific details into the subclasses. So your "fly()" method might depend on a class variable "topSpeed" which you'd declare in Bird and use in Bird.fly(), but you'd set it for each species. However, for a method like "seekDinner()", you'd just declare an abstract method at the top level and then implement it for each bird - a hawk would soar until it saw prey, then swoop down on it, a hummingbird would seek out a suitable flower, and a chicken would scratch for grubs, and those methods would be defined in each bird's class file.

(I'm using notional examples to make the point - you're probably not going to be concerned with what your Birds eat...)

commented: Good points. +13
Member Avatar for Amoryethel

Thank you so much for your responses. I have multiple birds in my client code, I just opted to exclude it and save some lines on my post. Each bird has a different color identifying it and a different movement, which is the sole purpose of the fly method. So would I include:

this.col = new Color();

in the Bird class, and include it in Hummingbird class like so?

public class Hummingbird extends Bird
{
   public Hummingbird(int x, int y)
   {
      super(x, y);
      this.col = new Color(210, 10, 55);
   }
   public void fly()
   {
      pos = new Point((int)(Math.random()* 19),(int)(Math.random() * 19));
   } 
}

Or just exclude:

this.col = new Color(210, 10, 55);

and

pos = new Point((int)(Math.random()* 19),(int)(Math.random() * 19));

Being that the above color and position is specifically for the Hummingbird class?

public class Hummingbird extends Bird
{
   public Hummingbird(int x, int y)
   {
      super(x, y);
      this.col = new Color(210, 10, 55);
   }
   public void fly()
   {
      pos = new Point((int)(Math.random()* 19),(int)(Math.random() * 19));
   } 
}

That looks like it's a reasonable piece of inplementation code for a reasonable Bird class. (although your hummingbird's movement is a little odd... it can be anywhere within a 19-unit square, and never outside of it?)

in Bird, you'd just declare col to be a Color:

Color col;

or possibly set a default value:

Color col = Color.YELLOW;

Since there's never going to be an instance of Bird, that's only important as a backstop, in case you forget to set Color in one of your Birds.

Likewise, pos would be declared

Point pos;

in your Bird class, unless you want to set a default starting point:

Point pos = new Point(0,0);

>> I have multiple birds in my client code, I just opted to exclude it and save some lines on my post. Each bird has a different color identifying it and a different movement, which is the sole purpose of the fly method.

Jon made some great points regarding the "abstract" modifier. There probably isn't any plain old "Bird", so consider making Bird abstract.

If there is no "default" behavior", make the method abstract in Bird and implement it in Hummingbird.

If there is a default behavior, put it in Bird.

If Hummingbird uses the default behavior, don't implement it in Hummingbird. Hummingbirds will then use the default.

If Hummingbird has its own behavior, write the function in Hummingbird. That's what will be used, regardless of whether there is a function with the same name in Bird.

Since every type of Bird has a different fly() behavior, every type should have its own fly() function.

Ditto on the color. If there's special handling involved for Hummingbirds, stick them in Hummingbird, either in the constructor or elsewhere.

Java looks at the bottom, then looks upwards. If you have a Hummingbird object and fly() is called, it will look there for a fly() function. If it finds one, it uses it. If not, it looks in Bird. The same would be true in any other function, including functions that work with the color.

Member Avatar for Amoryethel

Thank you very much. You've both helped me out a great deal. I was able to sort out the rest.

although your hummingbird's movement is a little odd... it can be anywhere within a 19-unit square, and never outside of it?

The hummingbird's movement is random, anywhere from a range of (0,0) to (19,19).

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.