Tonno22
Tonno22

Reputation: 165

Trying to figure out Inheritance and polymorphism

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
            + '}';
}
}

Upvotes: 0

Views: 172

Answers (2)

John M
John M

Reputation: 1479

 // 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 + "}";
 }

Upvotes: 1

AnxGotta
AnxGotta

Reputation: 1006

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}

Upvotes: 0

Related Questions