i've create a Person superclass to represent the base details of Survivor/Infected test game i'm creating in java. in the Person SuperClass i have a method called constructPersonInfo that i want to work w/ any type of subclass object to return that respective type. here's the code:
public static Person constructPersonsInfo()
{
Scanner scan = new Scanner(System.in);
System.out.println("------- please enter Persons first name -------");
String firstName = scan.nextLine();
System.out.println("------- please enter Persons last name -------");
String lastName = scan.nextLine();
System.out.println("------- please enter Persons age -------");
int age = scan.nextInt();
boolean isMaleOrFemale = false;
System.out.println("------- please enter male / female for your Persons gender -------");
String male_female = scan.nextLine();
if(male_female == "male")
{
isMaleOrFemale = true;
} else if(male_female == "female")
{
isMaleOrFemale = false;
}
Person p = new Person(firstName, lastName, age, isMaleOrFemale);
return p;
/*
the 2 lines of code above are obviously wrong, but for the life of my cannot
figure out the proper way to format the code to return either SubClass object
*/
}
code in the Main thread
public static void Main(String[] args){
Survivor s = (Survivor) Person.constructPersonInfo();
Infected i = (Infected) Person.constructPersonInfo();
}
so what exactly is this beatiful code of mine trying to do? well in essence i am trying to write a method in the SuperClass that can return data for any SubClass object depending on which SubClass Object called that method. i understand the nasty part of the Code is where the method is hard-coded to return a Person object only, and that's obviously wrong but since i am relatively new i need to know the proper way to do this. thank you all for you input in advanced, and of course i'd be more than happy to provide more information if requested :)