0

i have a superclass Dinosaur with only one subclass Tyrano

i have Dinosaur with these attributes:

public Dinosaur(String name, String size, String movement, String diet, String terainType){
    ...
    //i've already made the setters and getters along with some helper functions
    }

and i have Tyrano with 2 additional attributes which are teeth and hobby

public Tyrano(String name, String size, String movement, String diet, String terainType, int teeth, String hobby){
    ...
    //made setters and getters with some helper functions
    }

now in my driver program i want to make an array type Dinosaur that will accept multiple subclasses of Dinosaur one of them which is the subclass Tyrano i don't know if its possible but my instructor said it is so here's what i did, this is on the main:

Dinosaur[] dinoList = new Dinosaur[9];
dinoList[0] = new Tyrano("Gary", "Large", "other dino Meat", "Land", 30, "singing");
int teeth = dinoList[0].getTeeth();
String hobby = dinoList[0].getHobby();
...//i also called the helper functions that were in Tyrano

it gets an error:

error: cannot find symbol
       dinoList[0].getTeeth();
                  ^
error: cannot find symbol
       dinoList[0].getHobby(); 
                  ^
...//along with same errors with the helper functions that were in Tyrano
...//it also happens when i call setters that were unique for the subclass Tyrano

and i don't know why it's doing this, i've double checked and i have no syntactical errors and i've already defined the helper functions, also those getters and setters, but there's no problem for the common ones which are found in the superclass Dinosaur

4 Answers 4

2

If getTeeth() and getHobby() do not exist for Dinosaur class, you can't call them from a reference to a Dinosaur. Even if the actual instance stored in dinoList[0] is a Tyrano, you can't access its unique methods without casting the reference to a Tyrano.

This will work :

if (dinoList[0] instanceof Tyrano) {
    Tyrano t = (Tyrano) dinoList[0];
    int teeth = t.getTeeth();
    String hobby = t.getHobby();
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you use the subclass from an array that contains its superclass, then you have access only to the methods that are available in the superclass, as these are the methods common among all subclasses.

public class Dinosaur
{
    private String hobby;

    public String getHobby() {
        return hobby;
    }
    ...
}

public class Tyrano extends Dinosaur
{
    private String teeth;

    public String getTeeth() {
        return teeth;
    }
    ...
}

Then you have the following

 Dinosaur dinosaur = new Tyrano();
 String hobby = dinosaur.getHobby(); //this works
 String teeth = dinosaur.getTeeth(); //this is compile error!
 if(dinosaur instanceof Tyrano) {
     Tyrano tyrano = (Tyrano) dinosaur; //casting
     teeth = tyrano.getTeeth(); //this works
 }

Comments

0

This is a typical mistake. The point is, you have an array of Dinosaurs which don't have teeth or hobbies, is like if you have an array of Person and one of this "person" is a Teacher... not all the Person are Teacher like not all the Dinosaur have teeth.

So, you need to ask your Dinosaur "are you a Tyrano?" and if the answer is "yes" then... ask him "then, like a Tyrano, what is your hobbie?".

Let me put that in code:

//are you a Tyrano?
if(Tyrano.class.isAssignableFrom(dinoList[0].getClass())){
        // so, I treat you like a Tyrano
        Tyrano tyranoDyno= (Tyrano) dinoList[0];
        //tell me... teeth and hobbies please?
        int teeth = tyranoDyno.getTeeth();
        String hobby = tyranoDyno.getHobby();
}

2 Comments

Why the preference for Tyrano.class.isAssignableFrom(dinoList[0].getClass()) instead of dinoList[0] instanceof Tyrano?
In the example is the same, just a personal preference. The main difference is that isAssignableFrom could chose a class in runtime.
0

When you create a subclass, the subclass inherits the fields and methods from its superclass. So Tyrano has everything that Dinosaur has, and a bit more (teeth and hobby).

You could make another class, say Stego, which also extends Dinosaur and adds a field for armour.

Now your array of dinosaurs could contain both Tyrano and Stego types (they are both dinosaurs so this is fine). But your program has no way of predicting which type they will be. Since Stego has no teeth or hobby, and Tyrano has no armour, you can't use these fields directly (instead do as Eran suggested). You are however free to use all the fields from Dinosaur, since all dinosaurs are guaranteed to have inherited these

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.