• 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

Query on Generics

 
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following piece of code but as per my understanding it shouldn't compile. I am not able to figure it out.



Following is the Test class :



Can anyone explain why addListWilder method compiles. The logic applied for compiling addListWild and addListWilder are same i believe.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adithya narayan wrote:Can anyone explain why addListWilder method compiles. The logic applied for compiling addListWild and addListWilder are same i believe.


I don't think so. The type in the first method is known to be some kind of List<Class>, so I don't see to much problem there, providing you supply it with a Class. The second, on the other hand, is an unknown type, so add() won't work.

But you should double-check the Generics tutorial to be absolutely sure.

Winston
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston's right. The first method takes a list of type Class<? extends BaseSessionBean>, so it contains class objects for classes that represent BaseSessionBean or a subclass. You're then adding EJB.class, which is a class object representing a subclass of BaseSessionBean. So there's no problem - that meets the requirements of the list.

The second method takes a list of an unknown type that is BaseSessionBean or a subclass. But you don't know which subclass, so it's not safe to add anything to it. So I'd expect that not to compile.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess i am missing something over here.

I am trying to understand the reason why it is compiling. I am trying to call the method using the following way :


The above thing compiles. I don't get how you are allowed to not specify a type of class while creating. According to the documentation:


but if i call it as follows (don't even know whether this should be the way to call the method):


it doesn't compile! Nor does the following :


I couldn't see an explanation for this in the tutorial.



 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adithya narayan wrote:The above thing compiles. I don't get how you are allowed to not specify a type of class while creating. According to the documentation:


But you are specifying a type: Class. that class itself is generic, which may be what is confusing you. But Class<? extends BaseSessionBean>, Class<EJB>, Class<String> all resolve to the same type: Class.

adithya narayan wrote:but if i call it as follows (don't even know whether this should be the way to call the method):
it doesn't compile! Nor does the following :


How is Mediator defined? And what compiler error are you getting? If Mediator isn't a subclass of BaseSessionBean then you won't be able to call the method because the signature doesn't match. If it is, something else is going on.
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

adithya narayan wrote:The above thing compiles. I don't get how you are allowed to not specify a type of class while creating. According to the documentation:


But you are specifying a type: Class. that class itself is generic, which may be what is confusing you. But Class<? extends BaseSessionBean>, Class<EJB>, Class<String> all resolve to the same type: Class.



I agree that i am specifying the generic type for List as Class; but i am not specifying generic type for Class. Is that acceptable with the compiler ? Why ?

You mean to say :

List<? extends Animal> listAnimal = new ArrayList<? extends Animal>(); isn't acceptable but
List<Class<? extends Animal?> listClassAnimal = new ArrayList<Class<? extends Animal>>(); is ok with the compiler. My question over here is doesn't it require the generic type of class to be specifically defined ? Is it because the compiler needs information about the first generic type only (in this case the generic type of list) ? Please explain.

Matthew Brown wrote: And what compiler error are you getting? If Mediator isn't a subclass of BaseSessionBean then you won't be able to call the method because the signature doesn't match. If it is, something else is going on.



Mediator is a sub-class of BaseSessionBean.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adithya narayan wrote:I am trying to understand the reason why it is compiling. I am trying to call the method using the following way :


Guys, PLEASE don't put excessively long lines inside code blocks. They're hard to read and it screws up the windowing.

That means:
1. Don't do it.
2. If you are quoting a piece of code that includes a very long line (the limit is around 100 characters): BREAK IT UP.

I've edited all your posts to remove them, but please remember this in future.

Winston
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I will keep that in mind from now onwards ..but i still don't understand the behavior which is going on in my code !
 
reply
    Bookmark Topic Watch Topic
  • New Topic