Page 1 of 1

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

Posted: Sun Nov 15, 2015 8:27 am
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?

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

Posted: Sun Nov 15, 2015 8:41 am
by admin
Because this option (option 4) says it is an abstract class. It is not an abstract class, it is an interface.

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

Posted: Thu Jun 30, 2016 1:29 pm
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 ??

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

Posted: Thu Jun 30, 2016 8:42 pm
by admin
Not sure what you want to correct. The given answer and the explanation are correct. Please read what option 4 says carefully.

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

Posted: Wed Jan 03, 2018 3:51 pm
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 ?

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

Posted: Wed Jan 03, 2018 7:07 pm
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.

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

Posted: Wed Jan 03, 2018 7:19 pm
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.

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

Posted: Wed Jan 03, 2018 7:40 pm
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.

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

Posted: Wed Jan 03, 2018 10:13 pm
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.

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

Posted: Tue Jun 26, 2018 11:28 am
by flex567
In the answer there is MyCheckEmployee class which has ";" at the end. That shouldn't be there ?

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

Posted: Tue Jun 26, 2018 1:07 pm
by admin
Yes, it is not required but it is not an error either.

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

Posted: Fri Jul 09, 2021 7:42 am
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?

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

Posted: Sat Jul 10, 2021 2:13 am
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)?

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

Posted: Wed Feb 28, 2024 1:43 pm
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.

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

Posted: Wed Feb 28, 2024 11:31 pm
by admin
No, an interface is an interface. It is not an abstract class.