File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics doubt" Watch "generics doubt" New topic

generics doubt

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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?

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later" someone unknown
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Because you have to implement the exact signature of the methods defined in your interface.

JDBCSupport - An easy to use, light-weight JDBC framework -
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Override: Signature[Method's name and argument] must be same.

and remember Generics are invariant [not Covariant]
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Aug 03, 2008
Posts: 9465

You can try Sun's Generics Tutorial and Angelika Langer's Generics FAQ...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
On thurday one guy posted a same problem .

This error occurs because of type erasure in generics. Look into it in google

To err is human,
To forgive is not company policy
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521

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

Joined: Sep 06, 2009
Posts: 255

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


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.

Salil Verma
I agree. Here's the link:
subject: generics doubt
It's not a secret anymore!