Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a subclass method from superclass
    primarykey
    data
    text
    <p>I am in an introductory java course and we just started learning about inheritance. I am working on a task that asks that we create a "Pet" superclass with name and age; and three subclasses, each with their own unique trait (I have chosen "Dog", "Cat", and "Bird"). After we have all these built, we are to create a Main class to test everything, and this is where I am running into problems. I am attempting to call the <code>get</code> methods for these unique traits within <code>Main</code>, but it seems to only find methods that are in the superclass.</p> <p>Here is the Main class:</p> <pre><code>public class Kennel { public static void main(String[] args) { // Create the pet objects Pet cat = new Cat("Feline", 12, "Orange"); Pet dog = new Dog("Spot", 14, "Dalmation"); Pet bird = new Bird("Feathers", 56, 12); // Print out the status of the animals System.out.println("I have a cat named " + cat.getName() + ". He is " + cat.getAge() + " years old." + " He is " + cat.getColor() + "When he speaks he says " + cat.speak()); System.out.println("I also have a dog named " + dog.getName() + ". He is " + dog.getAge() + " years old." + " He is a " + dog.getBreed() + " When he speaks he says " + dog.speak()); System.out.println("And Finally I have a bird named " + bird.getName() + ". He is " + bird.getAge() + " years old." + " He has a wingspan of " + bird.getWingspan() + " inches." + " When he speaks he says " + bird.speak()); } } </code></pre> <p>Here is my superclass </p> <pre><code>abstract public class Pet { private String name; private int age; // Constructor public Pet(String petName, int petAge) { this.name = petName; this.age = petAge; } // Getters public String getName() { return(this.name); } public int getAge() { return(this.age); } // Setters public void setName(String nameSet) { this.name = nameSet; } public void setAge(int ageSet) { this.age = ageSet; } // Other Methods abstract public String speak(); // toString @Override public String toString() { String answer = "Name: " + this.name + " Age: " + this.age; return answer; } } </code></pre> <p>And here is one of the subclasses (they all look the same and are having the same error)</p> <pre><code>public class Cat extends Pet { private String color; // Constructor public Cat(String petName, int petAge, String petColor) { super(petName, petAge); this.color = petColor; } // Getters public String getColor() { return(this.color); } // Setters public void setColor(String colorSet) { this.color = colorSet; } // Other Methods @Override public String speak() { return "Meow!"; } // toString @Override public String toString() { String answer = "Name: " + super.getName() + " Age: "+super.getAge() + " Color: " + this.color; return answer; } } </code></pre> <p>So what is happening is I can't get the main method to find the <code>cat.getColor()</code> method, or any of the other ones unique to the subclasses.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload