Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Type of the Generic List 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 "Type of the Generic List" Watch "Type of the Generic List" New topic
Author

Type of the Generic List

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Hi,

I was going through one of the examples in the K&B SCJP5 book.

Here is the code that i tried out:

In the TestGenerics class i call the method in the Dog class, the code is:

It does not compile as argument passed to the method does not match that with the method signature as in the Dog class.

And hence the error:

D:\JAVA\shekhar java\sudipto nw\TestA.java:35: addAnimal(java.util.List<Dog> in Dog cannot be applied to (java.util.List<capture of ? super Dog>
((Dog)dogList.get(0)).addAnimal(dogList);// this line does not compile
^
1 error



My question is the List dogList is of what generic type, as passing the list to the method in Dog class resulted in error and so it is not of type <Dog>. Please explain...

It is just a question so feel free to answer.

Thank you.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39



First of all, you broke the generics. DogList is not a list of dogs -- it's a list of some unknown type that could be either dogs, animal, or objects. In this code, you casted the return type to Dog. The reason you probably did it is because the compiler complained.

Forcing the type when the compiler complains is not a good idea. If you are going to cast when you disagree with the compiler, then why bother using generics?


But to answer your question....

Again, dogList is not a list of dogs -- it's a list of some unknown type that could be either dogs, animal, or objects. The addAnumal() method of the Dog class requires a list of dogs, it can't accept a list of animals, or a list of objects. Since, dogList may not be a list of dogs, the compiler is complaining again.

Henry
[ December 16, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

So now what if I want to call a method in the dog class on the object that I get from the list passed to the method as argument?

I my another code I face the same problem.
The code goes like this:


The error is :



D: \ JAVA\shekhar java \ sudipto nw \TestParlor.java:19 : cannot find symbol
symbol : method getRent()
location : class java.lang.Object
rent=item.getRent( ) ; //cannot find symbol getRent()
^
1 error



I understand what you say but how did i break Generics?

I am sorry I could not understand you... Please explain....

Thank you.
[ December 16, 2008: Message edited by: Sudipto Shekhar ]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

I think I did not understand the purpose of generics(from the usage of the generics in the classes I showed in the previous posts). Please guide me if I am wrong somewhere.

Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

I understand what you say but how did i break Generics?

I am sorry I could not understand you... Please explain....


Basically, with Generics, you are asking that the Java compiler type check everything for you. But you had to cast the return for the collection to Dog.

Why? Because after the type checking, the Java compiler determined that it can only guarrantee that it is of an object type that is returned. You disagreed and casted it to Dog. BTW, there is nothing wrong with *not* using generics. If you don't agree with the type checking, then don't use generics. To use it, but cast when you don't agree is silly.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

The error is :

D: \ JAVA\shekhar java \ sudipto nw \TestParlor.java:19 : cannot find symbol
symbol : method getRent()
location : class java.lang.Object
rent=item.getRent( ) ; //cannot find symbol getRent()
^
1 error


The type E can be any type -- not just CD and DVD types. So, if it is not a CD or DVD type, how is it going to call the getRent() method?

Henry
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Originally posted by Sudipto Shekhar:
So now what if I want to call a method in the dog class on the object that I get from the list passed to the method as argument?

I my another code I face the same problem.


In the previous code i posted, i get the error cannot find the symbol getRent(). When the object is of type CD why cant it find the method?

Please help...


Thank you very much for your help.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Ok I understand now.
Thanks for clearing the miss-conception.
Thank you very much.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by Sudipto Shekhar:
I think I did not understand the purpose of generics(from the usage of the generics in the classes I showed in the previous posts). Please guide me if I am wrong somewhere.



As mentioned, there is nothing wrong with not using generics. The purpose of generics is to help you at runtime, by doing type checks are compile time.

But if it's too difficult to apply to your application. Or if you have to cast, because you disagree with the type checks, it may be best to just not use generics.

Henry
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Ok I understand the point clearly.
The preparation of SCJP took me in depth to many nice topics.. generics is one of them.

Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type of the Generic List
 
Similar Threads
Generics problem- why not taking Animal, Object and super of Dog
Doubt in generics
super generic and for loop
Generics Problem
Generics - super