About Question enthuware.ocpjp.v7.2.1309 :

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

Moderator: admin

Post Reply
Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Contact:

About Question enthuware.ocpjp.v7.2.1309 :

Post 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?

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

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

Post 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.

nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

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

Post 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?

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

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests