About Question enthuware.ocpjp.ii.v11.2.3444 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
teodorj
Posts: 28
Joined: Tue Jun 19, 2018 10:27 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by teodorj »

For example,
if an abc.print module implements an org.printing.Print service interface defined in PrintServiceAPI module using com.abc.PrintImpl class,
then this is how its module-info should look:

Code: Select all

module abc.print{
	requires PrintServiceAPI; //required because this module defines the service interface org.printing.Print
	provides org.printing.Print with com.abc.PrintImpl;
}
I notice that

Code: Select all

requires PrintServiceAPI;
does not use module name same with package name (org.printing.PrintServiceApi).

Module name with same name as package is optional and best practice right?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by admin »

Yes, it is a best practice but not a requirement. In this case, the problem statement says that the module is PrintServiceAPI, so that is why the module abc.Print's module-info has "requires PrintServiceAPI;" clause.

pavvel
Posts: 12
Joined: Fri Dec 15, 2023 4:00 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by pavvel »

In Explanation we have a next example:

Code: Select all

module abc.print{
    requires PrintServiceAPI; //required    
    //because this module defines the service interface org.printing.Print
    provides org.printing.Print with com.abc.PrintImpl; 
}
Is this correct that, nobody can uses com.abc.PrintImpl, due to lack of the exports word?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by admin »

Nobody refers to PrintImpl class directly. Other modules who want to use the Print service use the Print interface only (which is in PrintServiceAPI module). So exporting the package containing PrintImpl from abc.print module is not required. In fact, it is not even recommended to export com.abc.PrintImpl from ab.print module because you don't want anyone to accidently start using PrintImpl directly.

dameest
Posts: 16
Joined: Sat Nov 30, 2024 5:27 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by dameest »

Hello, the response in this question assumes that the service is defined in another module, which is not stated in the question. Isn't it possible for a module implementing a service to also contains the interface?

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3444 :

Post by admin »

It is indeed possible for a module that defines the service to also provide an implementation and vice-versa. But that is not a requirement. The problem statement specifically say "implements a service", it does not say "defines the service interface" so it is best not to assume that. This is especially true about modules because the general practice is to keep the service definition and service implementation separate.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests