0

I'm doing an exercise on Inheritance and polymorphism, I have 3 seperate clasees, my main class, a super Animal class, and a sub Cat class. I've made overloaded constructors, getters and setters, and toString() methods in both Animal and Cat classes. I think I have the inheritance part down. Now I need to make 2 Animal Object references, both an instance of Cat, example: one a type Siameese with a name Tobbie.

Could anyone give me an example of one of these object references? You can see I've attempted in my Main class there, but I'm not sure if that is correct.

Here are the three different classes I have currently.

public class Hw02 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Animal Siamese = new Cat("Tobbie");
}
}

Here's my Animal Class.

public class Animal {

private String name;

public Animal() {
    this("na");
}

public Animal(String name) {
    this.name = name;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "Animal{"
            + "name="
            + name
            + '}';
}
}

And here is my Cat class.

public class Cat extends Animal {

private String type;

public Cat() {
}

public Cat(String type) {
    this.type = type;
}

public Cat(String type, String name) {
    this.type = type;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

@Override
public String toString() {
    return "Cat{"
            + "type="
            + type
            + '}';
}
}
5
  • I think you have correctly instantiated the cat object. Now if you were to call Siamese.toString() in your main method, it will call the toString method of the Cat Class. Commented Jan 28, 2014 at 16:08
  • Just a hint, you forgot to initialize the field name in the constructors of class Cat. You should call the constructor of the superclass by using super(name); as the first line on your Cat constructor. The one that has a name, of course. Commented Jan 28, 2014 at 16:10
  • Also, the overriden method toString() in class Cat will only display the cat's type, and not its name. Commented Jan 28, 2014 at 16:11
  • Why will it only display the cats type? Commented Jan 28, 2014 at 16:15
  • Because the return statement in the toString method in the cat class does not return the name. Commented Jan 28, 2014 at 16:18

2 Answers 2

1
 // in main method
 Animal tobbie = new Cat("siamese", "Tobbie")
 Animal jackie = new Cat("tomcat", "Jackie")

 // in Cat class
 public Cat(String type, String name) {
      super(name)
      this.type = type;
 }

A few comments:

  • It is not proper convention to have the name Siamese; variable names should be "camelCase" (start with a lower-case letter). Compiler will accept it is as you have written, but it is a bad practice.
  • Your Cat(String type, String name) constructor didn't invoke the proper superclass constructor, thus type was lost; same for the Cat(String type) constructor
  • I think I would make Animal abstract and its constructors protected. I think it is a bad practice to let clients directly instantiate Animals without specifying what kind of animals they are.

Edit: Like this:

 Animal animal = new Animal("What am I?")

However, I don't consider it a good practice to do this, probably what you want done is better achieved otherwise.

Edit:

Cat toString():

 public String toString() {
     return super.toString() + " Cat{type=" + type + "}";
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Ah thank you very much, how could I make an Animal object reference of instance animal, with a name of "What am I?"?
You are welcome. Also, try to walk through your code in debugger to ensure you understand very well how it works. If noone's taught you debugging yet, take the time to learn it, it will pay off.
0

With the code you have above, this is an example:

Animal animal0 = new Cat("Siamese", "Bob");
Animal animal1 = new Cat("Tomcat", "Frank");
Animal animal2 = new Cat("Tomcat", "George");
Animal animal3 = new Animal("Elephant");

System.out.print(animal0.toString());
System.out.print(animal1.toString());
System.out.print(animal2.toString());
System.out.print(animal3.toString());

Would produce the output:

Cat{type=Siamese}
Cat{type=Tomcat}
Cat{type=Tomcat}
Animal{name=Elephant}

1 Comment

I want it to produce something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.