I need some help with classes and methods.
Here is what i need to do:
1. Create a concrete class, Robin, which inherits from Bird. Robin has a single instance variable, name, of type String with private access. Robin implements the remaining required methods.

Here is the code for each class:

public class AbstractTest
{
	public static void main(String[] args)
	{
		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");

		System.out.println("For the cat: ");
		System.out.print("This is: "); cat.describe();
		System.out.print("sound:   "); cat.sound();
		System.out.print("Sound:   "); cat.sleep();
		System.out.print("Moving:  "); cat.move();
		System.out.println("\n");

		System.out.println("For the robin: ");
		System.out.print("This is:  ");  bird.describe();
		System.out.print("Sound:    ");  bird.sound();
		System.out.print("Sleeping: ");  bird.sleep();
		System.out.print("Moving:   ");  bird.move();
		System.out.println("\n");

		System.out.println("\nEnd of program.");
	}
}
public abstract class Animal
{
	String type;

	public Animal(String type)
	{
		this.type = new String(type);
	}

	public abstract void describe(); // Abstract method
	public abstract void sound();	  // Abstract method
	public abstract void sleep();     // Abstract method
	public abstract void move();      // Abstract method
}
public abstract class Bird extends Animal
{
  protected String breed;    // Bird breed

  public Bird(String aName, String aBreed)
  {
    super("Bird"); // Call the base constructor
    breed = aBreed;
  }

  // Show a Bird's details
  public void move()
  {
    System.out.println("This " +breed+ " flies up and away!");
  }
}
public class Cat extends Animal
{
  private String name;     // Name of a Cat!
  protected String breed;    // Cat breed

  public Cat(String aName)
  {
    super("Cat");         // Call the base constructor
    name = aName;         // Supplied name
  }

  public Cat(String aName, String aBreed)
  {
    super("Cat");         // Call the base constructor
    name = aName;         // Supplied name
    breed = aBreed;
  }

  // Show a Cat's details
  public void describe()
  {
    System.out.println(name + ", a breed of "+type+" called " + breed);
  }

  public void sound()
  {
    System.out.println("Meow");
  }

  public void sleep()
  {
    System.out.println(name+" is having purrfect dreams!");
  }

  public void move()
  {
    System.out.println("This little kitty moves fast!");
  }

  public String getName()
  {
      return name;
  }
}
public class Robin extends Bird
{
	 private String name;     // Name of a Bird!

	 public Robin(String aName)
	   {
	     super("Robin");         // Call the base constructor
	     name = aName;         // Supplied name
	   }
	 public void describe()
	 {
	    System.out.println(name + ", a breed of " +type+ " called " + breed);
	 }

	 public void sound()
	   {
	     System.out.println("Tweet Tweet!");
	   }

	 public void sleep()
	   {
	     System.out.println(name + " the " +breed+ " sleeps with one eye open for the cat!");
  		}
	 public String getName()
	 {
	     return name;
  	 }
 }

This is the part I'm having trouble with:

super("Robin");         // Call the base constructor

For some reason the instance variable refuses to work. It works for Cat and Bird, but just doesn't want to work for Robin. What am I doing wrong?

Recommended Answers

All 5 Replies

The superclass is Bird, which doesn't have a constructor that just takes a name as the only parameter. (it's public Bird(String aName, String aBreed))

The superclass is Bird, which doesn't have a constructor that just takes a name as the only parameter. (it's public Bird(String aName, String aBreed))

I changed it and I'm still getting the same error.

"refuses to work" is neither a runtime nor a compile time error.
What error are you actually getting (if any)?
And if none, why do you think you're getting none?

I'd like to help more, but jwenting is right...
"the instance variable refuses to work" and
"I changed it and I'm still getting the same error"
just isn't enough info to work with.
Think about it: all you have told us that a changed version of the code (ie one we have no access to) has an instance variable that refuses to work. I can promise you that Java instance variables always work exactly as the language definition says they should, so presumably you mean "didn't do what I expected", but what was the unexpected behaviour?
We need exact error messages and line numbers and the exact version of the corresponding code please. If there's no error message we need to know exactly what you expected to happen, and exactly what actually happened.

Just replying to this thread to say that I solved this project on my own. I was messing up the parameters with aName/aBreed.

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.