About Question enthuware.ocajp.i.v8.2.1470 :

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

Moderator: admin

Post Reply
RAZER-KIEV
Posts: 17
Joined: Thu Oct 01, 2015 4:06 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1470 :

Post by RAZER-KIEV »

Why option "public abstract boolean test(T t);" is not correct? As I remember, by default all methods in interface are abstract, aren't?

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

Because this option (option 4) says it is an abstract class. It is not an abstract class, it is an interface.
If you like our products and services, please help us by posting your review here.

pritesh
Posts: 1
Joined: Thu Jun 30, 2016 1:46 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by pritesh »

Answer is (Option 2) and not the (Option 4).
Predicate is a interface and not a Abstract class,Hence it's methods are default Abstract.

Can please someone Confirm and Correct that ??

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

Not sure what you want to correct. The given answer and the explanation are correct. Please read what option 4 says carefully.
If you like our products and services, please help us by posting your review here.

hsiaok
Posts: 7
Joined: Tue Aug 22, 2017 11:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by hsiaok »

iterator() is listed as both instance method and abstract method of interface Iterable, like this:

Iterator<T> iterator()
Returns an iterator over elements of type T.
Returns:an Iterator


Under Abstract Method and Instance Method.

I thought an interface has 3 type of methods: default method, static method and abstract method; so my question is why is iterator() is a instance method under Iterable API ?

hsiaok
Posts: 7
Joined: Tue Aug 22, 2017 11:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by hsiaok »

I see there ArrayList has implemented iterator() method;
but I still don't understand instance method iterator() under Iterable interface API.

Please explain.
Thanks.

hsiaok
Posts: 7
Joined: Tue Aug 22, 2017 11:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by hsiaok »

I've look at it again.
Under interface API, the instance method is a combination of
Default Method and Abstract method.

Hope I had answered my own question correctly.
Thanks.

hsiaok
Posts: 7
Joined: Tue Aug 22, 2017 11:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by hsiaok »

hasNext() is an abstract method in Iterator interface can be instantiated by Iterable interface which is implemented by ArrayList.

But I don't find hasNext() method in ArrayList.
So, how can a Iterator interface reference call hasNext() ?

like:
Iterator<Employee> i = DataList.iterator();
while(i.hasNext()) {
....
}

Can someone please explain ?
Thanks.

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

Where you do see ArrayList implementing Iterator interface?? Check out the API here: https://docs.oracle.com/javase/8/docs/a ... yList.html
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Also, in your code, I don't see you calling hasNext() on an ArrayList object. You are calling it on an Iterator object. This Iterator object, which is returned by calling iterator() method on the ArrayList object, does implement hasNext.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by flex567 »

In the answer there is MyCheckEmployee class which has ";" at the end. That shouldn't be there ?

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

Yes, it is not required but it is not an error either.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by enthunoob »

Hello, I apologize in advance for my beginners question. It shows my basic understanding is limited, but I am eager to understand it. In the given example a separate interface is created. The explanation reads
it is a very common requirement
. But as the interface doesn't do anything, why can it not be left out? Thus the example code becoming:

Code: Select all

// commented out
// interface CheckEmployee {
//   boolean check(Employee e );
// }

//only changed CheckEmployee in argument list to MyCheckEmployee 
public void filterEmployees(ArrayList<Employee> dataList, MyCheckEmployee p){
   Iterator<Employee> i = dataList.iterator();
   while(i.hasNext()){
        if(p.check(i.next())){
             i.remove();
    }
   }
}

//implementation commented out
class MyCheckEmployee {    //implements CheckEmployee{
   public boolean check(Employee e){
       return e.getSalary()>100000;
   }
};

//no changes
filterEmployees(employeeList, new MyCheckEmployee());
This code would do the exact same thing, right? The only reason I can think of now, as to why the interface class is "required", is that it can be seen as handy when other test-classes need to be written (f.e. testing salary above 100,000). And ofcourse the interface CheckEmployee is required in this example to show the connection with Predicate interface. But is it otherwise required as well? With that, what I mean to ask:

Can
This is a very common requirement across applications.
be corrected to
This is a very common use across applications.
(or instead of use, 'implementation')? Or would that be incorrect?

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

>The only reason I can think of now, as to why the interface class is "required", is that it can be seen as handy when other test-classes need to be written (f.e. testing salary above 100,000).

The requirement refers to the fact that many applications include code that does some kind of filtering/selection of objects available in a collection. For example, while showing a list of items that satisfy a criteria, the application code will need to filter a list of items. So that's the requirement. The requirement is not to have an interface or a class. The requirement is of a feature.

The explanation shows two ways how this requirement can be met - 1. using a customized interface or a customized application specific class. 2. using a standard Predicate interface.

The fun part in software development is to implement something with least amount of new code. You can leave out the Predicate interface and write your own but why would you? and why should a million other developers write a similar class when the job can be done as easily by the Predicate interface (which is already a standard interface available in the Java library)?
If you like our products and services, please help us by posting your review here.

cksylr
Posts: 3
Joined: Tue Feb 20, 2024 12:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by cksylr »

admin wrote:
Sun Nov 15, 2015 8:41 am
Because this option (option 4) says it is an abstract class. It is not an abstract class, it is an interface.
Is interface not an abstract class? I think option 2 and option 4 is equal.

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

Re: About Question enthuware.ocajp.i.v8.2.1470 :

Post by admin »

No, an interface is an interface. It is not an abstract class.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 40 guests