简体   繁体   中英

Java/Android - GettingClassCast exception casting a base class to a subclass to invoke the subclass methods

So I have a GeneralTemplate class and an Exercise class which extends GeneralTemplate.

As can be seen in the GeneralTemplate code I have included, each class extending it (of which there are several) contain an ArrayList

When I get to this line...

Exercise chosenExercise = (Exercise) FitnessApp.routineList.get(chosenRoutinePosition).getItem(chosenWorkoutPosition).getItem(chosenExercisePosition);

I get the following error ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise

I don't understand why this is as Exercise is a subclass of GeneralTemplate?

GeneralTemplate code:

public class GeneralTemplate
{
    private String name;
    private ArrayList <GeneralTemplate> items;  // Generic items list

    // Super constructor for all subclasses
    public GeneralTemplate(String name)
    {
        this.setName(name);
        items = new ArrayList<GeneralTemplate>();
    }

    // Only sets will differ
    public void addItem(String newName)
    {
        items.add(new GeneralTemplate(newName));
    }   

    // Remove item at position
    public void removeItem(int position)
    {
        if (items.size() > 0 && position <= items.size())
        items.remove(position);
    }

    // Remove all items
    public void removeItems()
    {
        items.clear();
    }

    /* ****************** GETTERS AND SETTERS START ********************/

    // Get item
    public GeneralTemplate getItem(int position)
    {
        return items.get(position);     
    }

    // Set list of objects e.g Routines, Workouts, Exercises
    public void setItems(ArrayList <GeneralTemplate> items)
    {
        this.items = items;
    }

    // Return item list
    public ArrayList <GeneralTemplate> getItems()
    {
        return items;
    }

    // Return name
    public String getName()
    {
        return name;
    }

    // Set name
    public void setName(String name)
    {
        this.name = name;
    }
    /* ****************** GETTERS AND SETTERS END ********************/
}

Exception code where attempt to cast GeneralTemplate to exercise to invoke methods

chosenRoutinePosition = getIntent().getIntExtra("chosen_routine", 0);
chosenWorkoutPosition = getIntent().getIntExtra("chosen_workout", 0);
chosenExercisePosition = getIntent().getIntExtra("chosen_workout", 0);

// Navigates through the Routine List and gets the chosen routine
chosenExercise = (Exercise)FitnessApp.routineList.get(chosenRoutinePosition).getItem(chosenWorkoutPosition).getItem(chosenExercisePosition);

The Exercise class is very simple and extends GeneralTemplate with a few additional methods

Been a bit of a stumbling block this, any help would be amazing.

Cheers

ClassCastExceptionjava.lang.ClassCastException: com.karibastudios.gymtemplates.GeneralTemplate cannot be cast to com.karibastudios.gymtemplates.Exercise

In Java you can only cast in one direction. For example: a ListView is a View, but not all Views are ListViews (it could be a Button, a RelativeLayout, etc).

So you can go from an Exercise to a GeneralTemplate, but not the other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM