This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type of the Generic List

 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand now.
Thanks for clearing the miss-conception.
Thank you very much.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic