Suppose you have a class called Person
:
class Person {
public static Person createWithFirstName(String firstName) {
return new Person(firstName, null, null);
}
// etc. - more factory methods
// private constructor
private Person(String firstName, String lastName, String nickname) { }
// useful method
public String getDisplayName() { }
}
It's all good and dandy. But now you also need a class called Programmer
, and you suddenly realize the programmers are persons too!
But all of a sudden, you can't just
class Programmer extends Person { }
since Person
doesn't have any public
constructors.