• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Name clash - Type erasure

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
The following program was taken from K&B Bonus Master Exam, but i have made a lot of modification.

<blockquote>code:
<pre name="code" class="core">class Base
{
public List<?> asList(List<?> x) { return null; } //line 1
}
class Derived extends Base
{
public List<String> asList(List<String> x) { return null;} // line 2
}
</pre>
</blockquote>

Compilation error :

name clash: asList(java.util.List<java.lang.String> in Derived and asList(java.util.List<?> in Base have the same erasure, yet neither overrides the other

In this case, does Derived's asList() override Base's asList() ?
How is actually "type erasure" applied in the code ? What part of it raised the error ?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<blockquote>code:
<pre name="code" class="core"> class Base{
public List<?> asList(List<?> x)
{ return null; } //line 1}

class Derived extends Base{
public List<String> asList(List<String> x) {
return null;} // line 2
}
</pre>
</blockquote>

After type erasure, both the methods will be having same signature, hence no override, therefore compilation error.
[ July 15, 2008: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2"> class Base{
public List<?> asList(List<?> x)
{ return null; } //line 1}

class Derived extends Base{
public List<String> asList(List<String> x) {
return null;} // line 2
}
</font></pre><hr></blockquote>

After type erasure, both the methods will be having same signature, hence no override, therefore compilation error.

[ July 15, 2008: Message edited by: Chandra Bhatt ]



What's the problem here? I couldn't get it..
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

both the methods will be having same signature, hence no override

I thought having the same signature is a precondition for overriding ?? Am I totally wrong ?

When I was studying for SCJP 1.5, I took some mock exams from www.javablackbelt.com and had been confronted with similar questions. But in my experience, type erasure is beyond the scope of SCJP.

I wouldn't be sure if it really helps for SCJP going into depth there. But what do the others mean ?
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with Ralph on the "same signature being a pre-requisite for overriding methods"
can some1 explain the reason for compilation error?
 
Faber Siagian
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i change the code :

<blockquote>code:
<pre name="code" class="core">class Base
{
public List asList(List x)
{
return null;
} //line 1
}

class Derived extends Base
{
public List asList(List x)
{
return null;
} // line 2
}
</pre>
</blockquote>

There should be no problem if the code is compiled.
I think, code above is the result of type erasure, because all of the type are removed. But then why i get compiler error if i use the code with the type argument ?
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!