About Question enthuware.ocpjp.i.v11.2.3098 :

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

Moderator: admin

Post Reply
javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

About Question enthuware.ocpjp.i.v11.2.3098 :

Post by javabean68 »

Hi,

I have difficulties to understand why the first option doesn't override correctly the given method:

Code: Select all

public <T extends CharSequence> Collection<String> transform(Collection<T> list)

Code: Select all

public  Collection<String> transform(Collection<String> list) { return new HashSet<String>(); }; //1
It seems to me that //1 is actually an implementation of the given method with T=String...Is that perhaps the problem? One cannot give an explicit parametrization of a generic method to avoid collisions with the 'compiler-generated' one?

Thanks for any hint
Regards
Fabio

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

Think about it from the perspective of the user of this method. The base class's method's argument is transform(Collection<T> list), where T extends CharSequence, which means one could pass any CharSequence. So, they could pass a StringBuilder also.

But the subclass method is trying to break the contract by saying it will only accept a String. What if, based on the promise of the base class has code like this:
void methodX(Base b){
Collection<StringBuilder> c = ...;
b.tansform(c);
}


Now, if someone calls methodX with an object of type Derived, what will happen?
If you like our products and services, please help us by posting your review here.

javabean68
Posts: 31
Joined: Wed Mar 16, 2016 8:38 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by javabean68 »

Ok...and it could not be seen as an overload? The signature is different...

I tried with something like:

Code: Select all

class GenericsFoo {
	public <T> void m(T a){
		System.out.println("In GenericsFoo " + a);
	}
}


public class Generic extends GenericsFoo {	
	
	public void m(String a){
		System.out.println("In Generics " + a);
	}
	
	
	public static void main(String[] args){
		Generic foo = new Generic();
		
		foo.m("Hello!");
		
	}
}
and it gets compiled...What is the difference with the first option...I give an explicit implementation of a generic method...

But the compiler seems not to take this in account :-) What am I missing?

Thank you very much for your explanation and your Mock: I'm going to take the test tomorrow!

:roll:

Ciao
Fabio

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

Please read my explanation again above.
If you like our products and services, please help us by posting your review here.

dgangan
Posts: 2
Joined: Sun Apr 18, 2021 3:40 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by dgangan »

Hi all,

Could you please advise on following as it's not clear for me:

Why //4 is correct override of transform method?

Code: Select all

class Base{
   public <T extends CharSequence> Collection<String> transform(Collection<T> list)
   { return new ArrayList<String>(); }  }

Code: Select all

public <T extends CharSequence> Collection<T> transform(List<T> list) { return new HashSet<T>(); }; //4
Overriding method returns type: Collection<T> which looks to be not covariant with base method return type: Collection<String>
As for me it looks like return method may return Collection of anything, while base method allows only Collection of String to be returned

Thank you in advance

dariusl
Posts: 1
Joined: Sun Apr 18, 2021 11:32 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by dariusl »

dgangan wrote:
Sun Apr 18, 2021 3:47 am
Hi all,

Could you please advise on following as it's not clear for me:

Why //4 is correct override of transform method?
I fell for this as well. 4 is an overload, not an override. The option states "//4 correctly overloads the method in Base.". Derived has two "transform" methods in option 4.

dgangan
Posts: 2
Joined: Sun Apr 18, 2021 3:40 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by dgangan »

dariusl wrote:
Sun Apr 18, 2021 11:35 am
dgangan wrote:
Sun Apr 18, 2021 3:47 am
Hi all,

Could you please advise on following as it's not clear for me:

Why //4 is correct override of transform method?
I fell for this as well. 4 is an overload, not an override. The option states "//4 correctly overloads the method in Base.". Derived has two "transform" methods in option 4.
Yes, i see overloads word now :D
Thank you

cjgiron
Posts: 14
Joined: Fri Sep 09, 2022 5:03 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by cjgiron »

Hi Admin,

Can you please explain the line "super is not allowed while defining bounded types" in the explanation about //5?

Code: Select all

public <T super String> Collection<T> transform(List<String> list) {
        return new HashSet<T>(); };//5
Why are upper bounds accepted but not lower bounds?

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

If T is a super class of String, then you can't pass List<T> as an argument when argument type is List<String>. (Because, for example, T could be Object and List<Object> will break List<String>).
But if T is a subclass of String you can pass List<T> for List<String>.
If you like our products and services, please help us by posting your review here.

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by aPerson »

Hi,

If I do the following:

Code: Select all

class Base {

    public <T> Collection<String> transform(Collection<String> list) {
        return null;
    }
}

class Derived extends Base {
    @Override
    public <T> Collection<String> transform(Collection list) {
        return null;
    }

}
it won't compile, but when I remove the <T> from the overriding method, it does compile. Can you please explain why?

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

The presence of <T> makes the method in the derived class a generic method, which prompts compiler to apply a different set of rules for overriding of generic methods (see the error message generated by the compiler). When there is no <T>, it is not a generic method and the specification allows a generic method to be overridden by a non generic method.
If you like our products and services, please help us by posting your review here.

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by aPerson »

Hi, thanks for the reply:) Is the following part of the explanation still correct then?

"Don't get confused by the presence of <T> in the code. The same rules of overriding still apply."

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

Yes, see the second point in the explanation:
Second, if it is a potential override, check the generic type specification of the parameters. If the overriding method does not use a generic type specification for the parameter type, then it is valid. The reverse is not valid i.e. the overriding method is allowed to erase the generic type specification but is not allowed to add a generic type specification if the overridden method does not have it. If both the methods have a generic type specification, then the specification must match exactly.
I wrote "different" because I was talking about the difference between <T> and no <T>. When you have <T> in both, the same set of rules apply. I know it is a complicated but go through the complete explanation. If you try to pick sentences in isolation, you may feel some things sound contradictory.
If you like our products and services, please help us by posting your review here.

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by aPerson »

Thanks a lot, I think I got it now:) I merged the Second step with the type parameter-part and some new wording to make it clearer for myself. Would this be accurate?

"Second, if it is a potential override, check the generic type specification of the type-parameter(with bounds if any) and actual parameters. If the overriding method does not use a generic type specification in these places, then it is valid. The reverse is not valid i.e. the overriding method is allowed to erase the generic type specification but is not allowed to add a generic type specification if the overridden method does not have it. If both the methods have generic type specification, then the specification must match exactly (this goes for both type-parameter and parameters). For example, if the overridden method has the parameter Set<Integer>, then the overriding method can use Set or Set<Integer>. But if overridden method has Set, then the overriding method must also have Set for a valid override.

The T in <T> is called as the "type" parameter. It is used as a place holder.........etc"

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

Re: About Question enthuware.ocpjp.i.v11.2.3098 :

Post by admin »

Yes, that looks correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests