Bryan
Bryan

Reputation: 87

how to initialize different SubClass types w/ same SuperClass Method?

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 :)

Upvotes: 1

Views: 64

Answers (1)

TNT
TNT

Reputation: 2920

I think the simplest way to do so is to create the object beforehand and pass it in as a method argument, using setter methods to set its properties. Something like this...

public static void main(String[] args) {
    Survivor s = new Survivor(/* args */);
    constructPersonsInfo(s);
}

public static void constructPersonsInfo(Person p) {
    // ...
    p.setFirstName(firstName);
    p.setLastName(lastName);
    p.setAge(age);
    p.setMaleOrFemale(isMaleOrFemale);
}

Otherwise, you could always go for reflection, since as is, casting would result in a ClassCastException. But it often can be complex and is not guaranteed to work depending on various factors:

public static void main(String[] args) throws Exception {
    Survivor s = constructPersonsInfo(Survivor.class);
}

public static <T extends Person> T constructPersonsInfo(Class<T> resultantClass)
    throws NoSuchMethodException, SecurityException, IllegalArgumentException,
           InstantiationException, IllegalAccessException,
           java.lang.reflect.InvocationTargetException {

    // ...

    return resultantClass
        .getConstructor(String.class, String.class, int.class, boolean.class)
        .newInstance(firstName, lastName, age, isMaleOrFemale);
}

Upvotes: 1

Related Questions