Page 1 of 1

About Question enthuware.ocpjp.v7.2.1309 :

Posted: Tue Jan 06, 2015 11:31 am
by Svetopolk
This snippet is from the explanation:

Code: Select all

public class TestClass   {
	public   <E extends CharSequence> Collection<E> getWordsStartingWith(
			Collection<E> input, char ch)
This is my modification:

Code: Select all

public class TestClass <E extends CharSequence> {
	public Collection<E> getWordsStartingWith(
			Collection<E> input, char ch)
I moved <E extends CharSequence> from the method to class. The class looks good, but the usage is wrong.
Collection<String> ac = getWordsStartingWith(a, 'a');
Error:
The method getWordsStartingWith(Collection<E>, char) in the type TestClass<E> is not applicable for the arguments (List<String>, char)
Could you explain the difference please?

Re: About Question enthuware.ocpjp.v7.2.1309 :

Posted: Wed Jan 07, 2015 11:26 am
by admin
The difference is that in your case the E will be typed at a class level. You cannot type it to anything at the method level, which is what is happening in the first case. What if you type the class level E to Integer, and then call (List<String>, char) on the object? That wouldn't make sense.

Re: About Question enthuware.ocpjp.v7.2.1309 :

Posted: Thu Apr 20, 2017 5:17 am
by nikitos
After reading OCJP 8 book (Selikoff) it is no understanding in <T extends ClassName> method declaration (and as I can see in class declaration too). From book only one trick question about instantiation of type (we can't call constructor for type, like: new TYPE() )

I discovered for myself from book wildcards (upper, lower, unbounded).

Read Generics and Collections book is too complicated and requires more time that I have.

Maybe it is greater explained in OCJP6/7? Or in something other place?

Could you suggest?

Re: About Question enthuware.ocpjp.v7.2.1309 :

Posted: Thu Apr 20, 2017 10:56 pm
by admin
nikitos wrote:After reading OCJP 8 book (Selikoff) it is no understanding in <T extends ClassName> method declaration (and as I can see in class declaration too). From book only one trick question about instantiation of type (we can't call constructor for type, like: new TYPE() )

I discovered for myself from book wildcards (upper, lower, unbounded).

Read Generics and Collections book is too complicated and requires more time that I have.

Maybe it is greater explained in OCJP6/7? Or in something other place?

Could you suggest?
Did you try reading this one: http://www.angelikalanger.com/GenericsF ... csFAQ.html It is very good.
Paul.