Page 1 of 1

About Question enthuware.ocpjp.v7.2.1305 :

Posted: Thu Dec 11, 2014 10:17 pm
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

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

Posted: Thu Dec 11, 2014 10:35 pm
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.

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

Posted: Mon Apr 27, 2015 7:12 am
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.

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

Posted: Mon Apr 27, 2015 7:26 am
by admin
There is no solution. You simply cannot have two methods which have the same erasure in a class.

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

Posted: Tue Apr 28, 2015 3:51 am
by itsriaz
Ok thanks.

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

Posted: Fri Jan 29, 2016 4:07 pm
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?

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

Posted: Fri Jan 29, 2016 9:09 pm
by admin
Well, yes, where ever the situation described in this question arises, it will cause a compilation failure.

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

Posted: Wed Jan 15, 2020 5:24 pm
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.

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

Posted: Thu Jan 16, 2020 7:27 am
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.