About Question enthuware.ocpjp.v7.2.1305 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

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

Post by renatumb »

This question left me a little bit confused...
If every class extends Object and I can OVERLOAD the equals like below:

Code: Select all

 public boolean equals (SomeClass x) { ... }
Why can't I OVERLOAD add of ArrayList ?

Code: Select all

 public boolean add(Object o) 
Also,
The overridden method can use a subclass for the parameters but not a superclass.
How come ?

For me, the overriding method can vary like this:

Code: Select all

<SubClassOrSameClass> methodX (AlwaysSameType var ) throws <SomeSub/UncheckedExceptionOrNothing>

Deleted User 621

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

Post by Deleted User 621 »

I think the explanation is deficient. The compiler prints:

Code: Select all

BookList.java:8: error: name clash: add(Object) in BookList and add(E) in ArrayList have the same erasure, yet neither overrides the other
public boolean add(Object o) {
               ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
That is, overriding is not possible, because the two methods have the same erasure. Note that the compiler replaces E with Object in the bytecode, because in generic ArrayList it's typed as "E extends Object". But because the compiler knows at compile-time that E really is Book, as intended by the programmer, this is NOT a valid override, because that would have to use the exact same type. For the same reasons associated with generics, the compiler cannot consider this an overload, because it would result in the same bytecode method.

This is my interpretation. Please correct, if anybody knows better. I believe the last sentence of the explanation "the overridden method can use a subclass for the parameters but not a superclass" is just plain wrong, like renatumb implied. Overriding is covariant for the return type but not for the parameters.

BTW I find this an extremely difficult question, if not the most difficult I encountered :-)

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

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

Post by admin »

You are right about the explanation being wrong about overriding. If the subclass uses a different class type for the parameter (whether subclass or superclass), it will not be overriding the base class's method. It will be a new method altogether.

This has now been fixed.

thank you for your feedback!
Paul.

Post Reply

Who is online

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