About Question enthuware.ocajp.i.v7.2.893 :

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

Moderator: admin

Post Reply
ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

About Question enthuware.ocajp.i.v7.2.893 :

Post by ewebxml »

4. Given:
interface Worker {
void performWork();
}

class FastWorker implements Worker {
public void performWork(){ }
}

You are creating a class that follows "program to an interface" principle.
Which of the following line of code will you most likely be using?
Select 1 option:


a. public FastWorker getWorker(){
return new Worker();
}
b. public FastWorker getWorker(){
return new FastWorker();
}
c. public Worker getWorker(){
return new FastWorker();
}
d. public Worker getWorker(){
return new Worker();
}

Answer: c
Explanation:
a. This will not compile because Worker is an interface and so it cannot be instantiated.
Further, a Worker is not FastWorker. A FastWorker is a Worker.
c. This is correct because the caller of this method will not know about the actual class of the object that is returned by this method.
It is only aware of the Worker interface. Hence, if you change the implementation of this method to return a different type of Worker,
say SuperFastWorker, other classes do not have to change their code.
d. This will not compile because Worker is an interface and so it cannot be instantiated
---------
I selected option B.
The following code that compiles.

interface Worker {
void performWork();
}

class FastWorker {

public static void main(String[] args) {
FastWorker fw = new FastWorker();
fw.getWorker();
}
public FastWorker getWorker(){
return new FastWorker();
}
}
-----
Why is option B. incorrect?

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

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by admin »

When you program to an interface, your return type is usually the interface name and not the class name. If you keep a class name as the return type (as in option B), you are exposing implementation details to the users, which can inadvertently be relied upon. You don't want that to happen because then you can't change your implementation without breaking other people's code.

HTH,
Paul.

UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by UmairAhmed »

What is meant by program to the interface ?

I am not an experienced IT guy, so that is why asking this question!

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

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by admin »

This article explains it very well.

Erik-Jan
Posts: 1
Joined: Thu Jul 24, 2014 9:36 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by Erik-Jan »

Where is it stated you need to know what "program to an interface" means? Because not knowing this means you can't answer the question correctly.

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

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by admin »

Official exam objectives are not very detailed. They do not list out every single thing. "program to an interface" is a part of OOP. We have included questions on this because candidates have reported getting a question on this concept.

HTH,
Paul.

dxie1154
Posts: 9
Joined: Sat Jan 14, 2017 5:01 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by dxie1154 »

The link seems to not work for the explanation of the "program to an interface" principle. Can you explain why answer choice A follows the program to an interface principle, and possibly explain the program to an interface principle?
Thanks.

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

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by admin »

Here are a few links that explain program to an interface principle:
http://stackoverflow.com/questions/2697 ... tions-mean
http://joshldavis.com/2013/07/01/progra ... face-fool/

Option A does not follow this principle because the return type of the method is a class rather than an interface.
Option C is correct because as explained in the explanation to this option: The caller of this method will not know about the actual class of the object that is returned by this method. It is only aware of the Worker interface. Hence, if you change the implementation of this method to return a different type of Worker, say SuperFastWorker, other classes do not have to change their code.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by AndaRO »

One similar exemplu is :
List myList = new ArrayList(); // programming to the List interface

This question has to link design patterns?

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

Re: About Question enthuware.ocajp.i.v7.2.893 :

Post by admin »

Yes, List myList = new ArrayList(); is also similar.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests