Hopefully, the following will make sense. I have a program with a superclass called vehicle. There are three classes that extend this class, tank, chopper and boat.
In my code, depending on what option the user chooses will create an instance of that class. The vehicle class has a method in it for setting up the stats called setStats(). The problem is, that when I try to call this method, my code won't compile because the instance of the object is created in the if statement.
if (P2Type == "Tank") {tank p2 = new tank();};
if (P2Type == "Boat") {boat p2 = new boat();};
if (P2Type == "Chopper") {chopper p2 = new chopper();};
//main game
//set stats
p2.setStats();
I know that you would normally set the data type of a variable outside of the if statement, but because this variable could be one of three options, I am not really sure how to get around this problem. Grateful for any advice.