File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics doubt

 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whi doesnt this work?

suppose i had Parent p=new Child();
p.m(new ArrayList<String>()); chose the method in child? please explain....why doesnt it compile?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have to implement the exact signature of the methods defined in your interface.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override: Signature[Method's name and argument] must be same.

and remember Generics are invariant [not Covariant]
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
everyone says that k&b explains genercis the best...but even after reading it 3-4 times am unable to solve many questions.....can anyone tell me where i can get generics questions and from where i can understand it better?

Thank you
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try Sun's Generics Tutorial and Angelika Langer's Generics FAQ...
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On thurday one guy posted a same problem .

This error occurs because of type erasure in generics. Look into it in google
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

import java.util.*;

class Test<T> {

public static void main(String args[]) {
Test t = new Test();
}
}

when the new object of Test is created is T replaced by Object?




 
Salil Vverma
Ranch Hand
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raju,
Generics provide compile time type safety, that means when the code is converted to the byte code all the type information is erased. At that time the strongly typed collection gets converted to old type less collections.

That means after the compilation of the following code

or


would get converted to following ..


which is same at the byte code level but at source code you are using the List as strongly type casted to different datatypes. Due to this conflict you get the compilation error in your code..

I hope this would clear your doubt.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic