Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 422
salxander Avatar asked Apr 05 '12 01:04

salxander


People also ask

How do you call a subclass method?

Base has only method() Subclass has method() and specialMethod() the method specialMethod() is the one I want to call. It's generally a bad idea. If you know the object is of type Subclass, then refer to it as that and you have no problem. If the method really belongs in Base - put it in Base.

How do you call subclass method from superclass in SAP ABAP?

A super class reference variable can hold a subclass reference variable . This superclass can call methods which are defined in the superclass only . data lr_animal type ref to zanimal. data lr_lion type ref to zlion.

How do you call a method in superclass?

Use of super() to access superclass constructor As we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use super() .

How do you define a subclass from a superclass?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.


2 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 
like image 199
Attila Avatar answered Sep 26 '22 02:09

Attila


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(...); 
like image 26
binarious Avatar answered Sep 25 '22 02:09

binarious