About Question enthuware.ocpjp.v7.2.1305 :
Moderator: admin
-
- Posts: 12
- Joined: Thu Dec 11, 2014 12:32 am
- Contact:
About Question enthuware.ocpjp.v7.2.1305 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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.
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.
-
- Posts: 10
- Joined: Tue Nov 18, 2014 10:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
Hi could you please help me with the statment
Thanks in advance.
How can we override it or how to solve such problem.both the methods are same after generic information is erased, so one should override the other
Thanks in advance.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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.
-
- Posts: 10
- Joined: Tue Nov 18, 2014 10:01 am
- Contact:
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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.
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1305 :
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.
Who is online
Users browsing this forum: No registered users and 60 guests