Page 1 of 1

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

Posted: Tue Jul 02, 2013 4:09 pm
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?

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

Posted: Tue Jul 02, 2013 4:29 pm
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.

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

Posted: Sat May 24, 2014 2:29 pm
by UmairAhmed
What is meant by program to the interface ?

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

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

Posted: Sat May 24, 2014 8:47 pm
by admin
This article explains it very well.

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

Posted: Thu Jul 24, 2014 9:38 am
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.

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

Posted: Thu Jul 24, 2014 9:48 am
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.

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

Posted: Sat Jan 14, 2017 5:05 pm
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.

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

Posted: Sat Jan 14, 2017 11:59 pm
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.

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

Posted: Sun Mar 05, 2017 2:32 pm
by AndaRO
One similar exemplu is :
List myList = new ArrayList(); // programming to the List interface

This question has to link design patterns?

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

Posted: Sun Mar 05, 2017 9:40 pm
by admin
Yes, List myList = new ArrayList(); is also similar.