Aug 07

This post is also available in: German

First of all an extended comprehension before we are going into the specials of generics; let’s start with the following declarations:

Employee () { … }
SalesPerson extends Employee () { … }
ControllingStaff extends Employee () { … }

A cast in an upper class is possible without problems:

Employee a = new SalesPerson ();
Employeer b = new ControllingStaff ();

Same is applicable for arrays:

Employee [ ] c = {new SalesPerson(), new ControllingStaff () };

But this is not working for Collections:

List <Employee> b = new ArrayList <SalesPerson>();

What is working is the assignment of sub-classes to the collection of the upper class :

List <Employee> d = new ArrayList <Employee>();
d.add(new SalesPerson());
d.add(new ControllingStaff());

No warning is produced, the code is working fine through the hole program-flow.

  • Print
  • Facebook
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • RSS
  • Blogosphere
  • email
  • PDF
  • Wikio
  • Technorati
  • Digg
  • DZone

2 Responses to “generic assignments”

  1. Pandiaraj says:

    Is this a question? If it is, I will explain the behavior.

  2. Silke Wingens says:

    Hi Pandirajaj,
    should be more an explanation than a question but you are very welcome to add something.
    Best regards

Leave a Reply

preload preload preload Blog Top Liste - by Blogverzeichnis - Blog Verzeichnis Software
Webbhotell Top Blogs