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?