• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type of the Generic List

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I understand now.
Thanks for clearing the miss-conception.
Thank you very much.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic