If I am understanding correctly:
\"A factory constructor affords an abstract class to be
instantiated by another class, despite being abstract.\"
>
I would see Cat
as the default implementation of Animal
.
Of course you can't do Dog dog = new Animal();
because new Animal();
returns a Cat
but you can do Dog dog = new Dog();
or Animal animal = new Dog();
EDIT to long for a comment
:) This is just one other example how you can utilize factory constructor. Try to see the factory constructor as a normal function (top level function or static class function) that returns an object. To make the user of the class unaware of what is going on behind the scene allow him to use it like a constructor with new SomeClass
. That is what a factory constructor is. The first use cases that comes to mind are usually an implementation for the singleton pattern or for caching purposes but also all other cases where it looks for the user of the class as if he just creates a new instance but in fact he gets something constructed and prepared in a way that is more complicated but he needs not to be aware of.