Page 1 of 1

About Question enthuware.ocpjp.v8.2.1788 :

Posted: Mon May 30, 2016 12:03 pm
by schchen2000
Further, the return type of a create method is usually an interface and not a class.
If the return type is Interface, then how can we create an object directly or straight out of an Interface by using new Interface_Name() as in

return new Student("000", "Unnamed");

in your 2nd answer option?

I'm not disputing the return type being an Interface. For example, Paths factory class give us a Path Interface as its return value.

If you had, say

Code: Select all

Student aha = new Student(){

// Do useful stuff here. This would be an anonymous inner class style of coding and Student can be EITHER an abstract class OR Interface.

}

return aha;
or if Student is a function interface, we can then use a Lambda Expression to generate an object out of an interface.

Hope we are on the same page on this.

Thank you.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Mon May 30, 2016 12:43 pm
by admin
Not sure I understand your question.
You can instantiate an anonymous class that implements the interface directly like this:

Interface i = new Interface(){
public void interfaceMethod1(){ };
public void interfaceMethod2(){ };
};

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.

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Mon May 30, 2016 4:00 pm
by schchen2000
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

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Mon May 30, 2016 9:04 pm
by admin
No, the explanation doesn't imply that Student is an interface. The explanation is giving you additional info that the return type of the factory method is USUALLY an interface. It is obviously not the case here because Student is defined as a class. The return type of a factory method CAN be a class as is the case here but is not the preferred way.

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Tue May 31, 2016 6:27 pm
by schchen2000
admin wrote:No, the explanation doesn't imply that Student is an interface. The explanation is giving you additional info that the return type of the factory method is USUALLY an interface. It is obviously not the case here because Student is defined as a class. The return type of a factory method CAN be a class as is the case here but is not the preferred way.
Ah.... I see.

The return type of the factory method is USUALLY an interface BUT NOT ALWAYS.

The return type of a factory method CAN be a class.

A good real-life example is

FileSystems (plural) is a factory class and it returns FileSystem (singular), which is a class. FileSystem is then used to generate a Path object.

Is that a good example of that?

Thank you very much for your time and good explanation.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Tue May 31, 2016 8:30 pm
by admin
That is correct.

Re: About Question enthuware.ocpjp.v8.2.1788 :

Posted: Tue May 31, 2016 10:49 pm
by schchen2000
Thank you.

Schmichael