I am going through Effective Java. The very first item makes a convincing case for static factory methods over constructors.
I didn't get the first disadvantage which is
The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
Why do i need to subclass the class having static factory method since static methods can't be inherited?
Can someone please explain.
Suppose you have a class called
Person
: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
since
Person
doesn't have anypublic
constructors.