admin wrote:
But that is not the point here. In a factory pattern, the factory method usually instantiates a regular class that implements the interface mentioned as the return type.
Code: Select all
class StudentFactory{
public Student createStudent(){
return new Student("000", "Unnamed");
}
}
Further, the return type of a create method is usually an interface and not a class.
What you said in blue above "the factory method usually instantiates a regular class that implements the interface mentioned as the return type" is indeed true, then the explanation code snippet in your answer choice 2 (shown in green above) is wrong. Please see my reasoning below.
There is only one instantiation in the above block of code in green, i.e. by using the keyword new.
There is only one return type in the above block of code in green, i.e. Student.
In that above block of code in green (i.e. your own explanation provided in your 2nd answer choice), you implied that Student is an interface by saying "the return type of a create method is usually an interface and not a class."
We've got only one return type, i.e. Student, and we've got only one create method, i.e. createStudent(), in the above block of code in green. So, that means Student is an interface, not a class.
My question to you is how can you use the keyword new DIRECTLY on an interface, i.e. by doing new Student().
To the best of my knowledge, you CANNOT use new DIRECTLY on an abstract class either.
Hope that makes sense to you. Thank you.
Schmichael