473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Subtype in Generics?

dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. List<Number> li1 = ..;
  2. List<Integer> li2 = ..;
  3. li1 = li2; //li1 can't be subtype of li2
  4.  
Explain: If li1 gets updated with the Sub Class of Number and which is not integer then accessing li2 results runtime error.

Then ..
Expand|Select|Wrap|Line Numbers
  1. List<?> li1 = ..;
  2. li1 = li2; //it's valid
  3.  
Here you can't update li1 except null.

Expand|Select|Wrap|Line Numbers
  1. List<? extends Number> li1 = ..;
  2. li1=li2; //it's also valid
  3.  
Here you can't also update li1 except null for the same reason(explained earlier).

So with the valid codes li1 can't be updated. Then why ? extends SuperClass. What's the problem with first code fragment?
May 2 '09 #1
1 1665
JosAH
11,448 Expert 8TB
Read (especially) Chapter 3 from this link.

kind regards,

Jos
May 2 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Konstantinos | last post by:
I cant find much info or a code on how to do this. I have a generic table called Contact. Then I have tables Person and Company that are subtypes of contact each with attributes that are specific...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: xsnox | last post by:
anyone?? am trying to create a database (mysql) from a super type subtype relation. my question is how do i create the tables with the right constrain (PK, FK) here is the proposed tables t ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.