About Question enthuware.ocpjp.v7.2.1305 :

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

Moderator: admin

Post Reply
rocky_bgta
Posts: 12
Joined: Thu Dec 11, 2014 12:32 am
Contact:

About Question enthuware.ocpjp.v7.2.1305 :

Post by rocky_bgta »

import java.util.*;

class Book {
}

class TextBook extends Book {
}

class BookList extends ArrayList<Book> {
public int count = 0;

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

add(Object) in BookList and add(E) in ArrayList have the same erasure, yet neither overrides the other.
I don't understand what the above line mean?
Thanks
Md. Nazmus Salahin Rocky

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

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

Post by admin »

If you think about it conceptually, it will be easy -
1.you know the all generic related information is removed or erased by the compiler, right? The JVM has no idea about generics because bytecode has no generics related information in it.

2. ArrayList has a generified add method, which will translate to the bytecode as add(Object obj).

When you extend ArrayList and create your own add method, you get two add methods - one from ArrayList and one in the subclass - in your subclass. Ideally, it should be ok because the subclass method will override the base class method. However, the method that you are creating in the subclass is indirectly typed to Book (because your class is typed to Book). The compiler now realizes that the two methods are kind of different - one is add(E) and another one is add(Book ) but once compiled both will look the same after translation i.e. add(Object ).

This situation is prohibited by the language designers to prevent developers from writing confusing and error prone code. That is what the error message says, both the methods are same after generic information is erased, so one should override the other, yet, in the code their parameter list is different so neither one overrides the other.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

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

Post by itsriaz »

Hi could you please help me with the statment
both the methods are same after generic information is erased, so one should override the other
How can we override it or how to solve such problem.
Thanks in advance.

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

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

Post by admin »

There is no solution. You simply cannot have two methods which have the same erasure in a class.
If you like our products and services, please help us by posting your review here.

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

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

Post by itsriaz »

Ok thanks.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

Post by krohani »

So will this situation occur everytime you attempt to extend a typed generic class? Anytime you extend a typed generic class and you try to override a method in the base class you will get a type erasure compilation error?

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

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

Post by admin »

Well, yes, where ever the situation described in this question arises, it will cause a compilation failure.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

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

Post by Bhaskar »

Please verify if my understating is correct

Since ArrayList<Book> is typed to Book, its add method will also be typed to Book, which the BookList class inherits. Now at run-time, this method will be type-erased to Object. So basically the BookList class has two methods of same signature; one that is type-erased from ArrayList and other it's own. That's what's causing the name clash.

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

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

Post by admin »

Correct, the compiles notices that BookList gets two versions of the add method. Normally, it would be a valid override at runtime, but at compile time, the signatures of the two methods do not match and so the compiler complains.
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 31 guests