I'm trying to create a really simple Factory Method design pattern example in Java. I don't really know Java, I am new to programming in general but I need to come up with a a basic FactoryMethod example implemented in java. The following is what I came up with. There are quite a few errors I'm sure, I'm missing some constructors apparently and I get confused with abstract classes and interfaces. Could you point out my mistakes and correct my code along with an explanation please? Thank you in advance for your time and help.
public abstract class Person
{
public void createPerson(){ }
}
public class Male extends Person
{
@Override
public void createPerson()
{
System.out.print("a man has been created");
}
}
public class Female extends Person
{
@Override
public void createPerson()
{
System.out.print("a woman has been created");
}
}
public class PersonFactory
{
public static Person makePerson(String x) // I have no Person constructor in
{ // the actual abstract person class so
if(x=="male") // is this valid here?
{
Male man=new Male();
return man;
}
else
{
Female woman=new Female();
return woman;
}
}
}
public class Test
{
public static void main(String[] args)
{
Person y= new Person(makePerson("male")); // definitely doing smth wrong here
Person z= new Person(makePerson("female")); // yup, here as well
}
}