Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

java - 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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...