Reputation: 1091
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.
Upvotes: 29
Views: 102258
Reputation: 61
You can also add these empty methods to your Pet class:
public String getColor() { }
public String setColor() { }
public String speak() { }
This way you can call this method from the Pet object without having to cast it to a Cat. Once it is called, Cat's method overrides a method from superclass. The drawback of this trick is that you have to declare an empty method in your superclass for every method that you intend to call from a subclass, which might get messy if there are multiple methods to be added. So only do it for the methods that will get used in many different subclasses.
Upvotes: 0
Reputation: 99
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");
Upvotes: 1
Reputation: 28802
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
Upvotes: 37
Reputation:
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.
Upvotes: 1
Reputation: 4588
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(...);
Upvotes: 1