Calling a subclass method from superclass

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 get methods for these unique traits within Main, but it seems to only find methods that are in the superclass.

Here is the Main class:

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());       
    }
}

Here is my superclass

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

And here is one of the subclasses (they all look the same and are having the same error)

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

So what is happening is I can't get the main method to find the cat.getColor() method, or any of the other ones unique to the subclasses.

Answers


When you declare a variable as having the type of the superclass, you can only access (public) methods and member variables of the superclass through that variable.

Pet cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // this is OK
cat.getColor(); // this is not OK, getColor() is not in Pet

To access the methods in the concrete class (Cat in this case), you need to either declare the variable as the derived class

Cat cat = new Cat("Feline",12,"Orange"); 
cat.getName(); // OK, getName() is part of Cat (and the superclass)
cat.getColor(); // OK, getColor() is part of Cat

Or cast it to a type you know/suspect is the concrete type

Pet cat = new Cat("Feline",12,"Orange"); 
((Cat)cat).getName(); // OK (same as above)
((Cat)cat).getColor(); // now we are looking at cat through the glass of Cat

You can even combine the two methods:

Pet pet = new Cat("Feline",12,"Orange"); 
Cat cat = (Cat)pet;
cat.getName(); // OK
cat.getColor(); // OK

Pet cat = new Cat("Feline",12,"Orange");
^^^
This is the error.

Pet does not have a Method called getColor()

You need to do:

Cat cat = new Cat(...);

When you do this :

 Pet cat = new Cat("Feline",12,"Orange");

The compiler see the variable cat as Pet. So you can't use the specific method of Cat classe.

You have to declare cat as Type Cat to resolve your problem.

Regards.


Here you are trying to create Superclass type object(cat) using the constructor (Cat) which is a subclass method, that is not possible. this is violating the rule of Inheritance.

Pet cat = new Cat("Feline", 12, "Orange");


Need Your Help

How to make my app not to link to some lib? `QT -= `?

c++ qt visual-c++ linker qmake

So I try to compile my app statically. anf I do not link my app to QtScript.lib in my pro file. but something does. I compile using MSVC compiler. I wonder how to say in .pro file that my app sh...

Python how to update a dictionary with another dictionary by applying add to existing values

python dictionary

I have an existing dictionary with string keys and numerical values. I create another dictionary containing new keys and values which I need to update over the existing dictionary.