About Question com.enthuware.ets.scjp.v6.2.95 :

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

Moderator: admin

Post Reply
javatek202
Posts: 8
Joined: Tue May 14, 2013 12:27 am
Contact:

About Question com.enthuware.ets.scjp.v6.2.95 :

Post by javatek202 »

What is the best way to fix this compile error, which says
name clash: add(Object) in BookList and add(E) in ArrayList have the same erasure, yet neither overrides the other
?

Can you explain the error and how it would be fixed.

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

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by admin »

This thread explains it really well: https://forums.oracle.com/thread/1184741
If you like our products and services, please help us by posting your review here.

asi-aal
Posts: 10
Joined: Wed Nov 23, 2022 3:40 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by asi-aal »

Why method add(E e) can not be overloaded?
Following code won't compile

public boolean add(Object o)
{
if(o instanceof Book ) return super.add((Book) o);
else return count++ == -1;
}

public boolean add(Book o)
{
if(o instanceof Book ) return super.add((Book) o);
else return count++ == -1;
}

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

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by admin »

Please post complete code that you tried to compile along with the error message.
If you like our products and services, please help us by posting your review here.

JavaMara
Posts: 1
Joined: Sun Nov 26, 2023 11:15 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by JavaMara »

admin wrote:
Fri Sep 20, 2013 5:32 pm
This thread explains it really well: https://forums.oracle.com/thread/1184741
The link is no longer available, can you add the fix here please ?

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

Re: About Question com.enthuware.ets.scjp.v6.2.95 :

Post by admin »

ArrayList has an add(Object ) method. But in the given code, we are using ArrayList<Book>, which means that ArrayList has been parameterized to use Book and so, from the compiler's perspective, ArrayList<Book> now has as add(Book ) method (instead of add(Object ) ).

Next, we have defined BookList to extend from ArrayList<Book>, this means that if we want to override the add(Book ) method in the BookList class, we need to define it as add(Book ). If we define it as add(Object ), the compiler will consider it an overload (because base class has add(Book ) and subclass has add(Object ) ). However, this overload will not work for the JVM because generics are not reified. The JVM doesn't see the generic information and so it doesn't know that anything like ArrayList<Book> exists. It only knows about ArrayList.

So, now you can see that there is a contradiction - from compiler's perspective it is a overload but from the JVM's perspective it is an override. To avoid this contradiction, the compiler doesn't allow it.

If you are not sure about reification in generics, you will need to go through a good book to read about it first.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 108 guests