• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Generics extends keyword and getElement()

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Another Generics question from me. Thanks to Vierda Mila for answering my last few queries!



In the above case, <? extends Dog> indicates Cage can contain any subclass object of Dog. The RHS indicates that the object created of Cage needs to contain a type of class Poodle. Then why is it that any of the getElement() invocations do not show a compilation error.
According to my understanding, it is because, <? extends Poodle> allows addition of only null objects to the cage. Hence any retrieval using a getElement() expects a null object to be returned, which can be type-casted into any other class (Object/Dog/Poodle/Labrador). Please correct me if i am wrong.

Why i find this tough to understand is because i do not see a practical usage of the above syntax. Please let me know if there is any.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From where did the class Poddle come. You didn't show poddle class in your code...
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:From where did the class Poddle come. You didn't show poddle class in your code...



Hi Ankit,

Sorry, Please consider the following code as an addition to thecode above:

 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Namrata Ap wrote:In the above case, <? extends Dog> indicates Cage can contain any subclass object of Dog. The RHS indicates that the object created of Cage needs to contain a type of class Poodle. Then why is it that any of the getElement() invocations do not show a compilation error.



Well the RHS doesn't matter after the declaration. The compiler doesn't know with which type cg11 was declared. So RHS doesn't matter here


Namrata Ap wrote:According to my understanding, it is because, <? extends Poodle> allows addition of only null objects to the cage. Hence any retrieval using a getElement() expects a null object to be returned, which can be type-casted into any other class (Object/Dog/Poodle/Labrador). Please correct me if i am wrong.



Well you are right that under the current implementation, you can only send null as the value to the setElement method. But don't judge your answer on that basis. Actually if you see your code from a different perspective, then your code is working like this



So now the calling code will determine the return type of the method. I would recommend that you read sun's tutorial on generics to be more clear...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic