Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Generics Doubt

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I have a major doubt about generics related to the following code shown below.This code is a question from javabeat.net generics mock exam.
code:
class Basket<E> {
private E element;

public void setElement(E x) {
element = x;
}

public E getElement() {
return element;
}
}

class Fruit {
}

class Apple extends Fruit {
}

class Orange extends Fruit {
}
class GeneTest{
public static void main(String args[]){

Basket<Fruit> basket = new Basket<Fruit>(); // 1
basket.setElement(new Apple()); // 2
Apple apple = basket.getElement(); // 3
}
The answer to the question is that The line 3 however will cause a runtime error. The result type of the methode getElement of Basket<Fruit> is Fruit. We cannot assign a Fruit to a variable of the type Apple without a cast.
According to me the answer to this code would be that it generates a compiler error at line 2.Because i thought that Basket class is parametarized to contain only Fruits.even though Apple class is a subclass of the Fruit class i thought as generics go against subtyping it would cause an compiler error.
can anyone explain this concept in a better way..

Thank you..
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you compiled the code? What was the result?
 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeff
I tried compiling the code and it gives me a compiler error saying incompatible type at line 3 of the code.My doublt is in line 2 .can we assign a subclass reference to the Basket class using generics.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, absolutely -- this is upcasting.
 
It was the best of times. It was the worst of times. It was a tiny ad.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic