473,385 Members | 2,003 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,385 software developers and data experts.

Generic inheritance

Hello!

I have some classes below.
I have also a generic collection class with this class header definition.
public class Farm<T: IEnumerable<Twhere T : Animal {...}

Now to my question according to a book that I read it says:
"The type of this list is determined by the type parameter T that is passed
to Farm<Tand
is constraned to be or inherit from Animal."
Because class Animal is abstract it's not possible to create an instance of
that class.

So what does this where T : Animal mean
I assume it means that T can be of a type derived from Animal or Animal
itself but
in some cases as in my case Animal itself are not allowed because it's an
abstract class.

Does my conclusion sound reasonable?

public abstract class Animal {. . .}
public class Chicken : Animal {...}
public class Cow : Animal {...}
public class SuperCow : Cow {...}

//Tony

Jun 27 '08 #1
2 1526
On Jun 19, 8:40 am, "Tony" <johansson.anders...@telia.comwrote:
Hello!

I have some classes below.
I have also a generic collection class with this class header definition.
public class Farm<T: IEnumerable<Twhere T : Animal {...}

Now to my question according to a book that I read it says:
"The type of this list is determined by the type parameter T that is passed
to Farm<Tand
is constraned to be or inherit from Animal."
Because class Animal is abstract it's not possible to create an instance of
that class.

So what does this where T : Animal mean
I assume it means that T can be of a type derived from Animal or Animal
itself but
in some cases as in my case Animal itself are not allowed because it's an
abstract class.

Does my conclusion sound reasonable?

public abstract class Animal {. . .}
public class Chicken : Animal {...}
public class Cow : Animal {...}
public class SuperCow : Cow {...}

//Tony
Inheritance is a is-a relationship, so any class that derives from
Animal (directly or indirectly) can also be considered of the type
Animal. In other words: your assumption is correct.

Brian
Jun 27 '08 #2
Tony wrote:
Hello!

I have some classes below.
I have also a generic collection class with this class header definition.
public class Farm<T: IEnumerable<Twhere T : Animal {...}

Now to my question according to a book that I read it says:
"The type of this list is determined by the type parameter T that is passed
to Farm<Tand
is constraned to be or inherit from Animal."
Because class Animal is abstract it's not possible to create an instance of
that class.

So what does this where T : Animal mean
I assume it means that T can be of a type derived from Animal or Animal
itself but
in some cases as in my case Animal itself are not allowed because it's an
abstract class.

Does my conclusion sound reasonable?

public abstract class Animal {. . .}
public class Chicken : Animal {...}
public class Cow : Animal {...}
public class SuperCow : Cow {...}

//Tony
No that is not correct. The fact that the Animal class is abstract
doesn't disqualify it from being used as T in the Farm class. You can
still create a Farm of the Animal type:

Farm<Animalfarm = new Farm<Animal>();

In this farm you can have any object that is an Animal. You can't create
instances of the Animal class, of course, but anything that you put in
the Farm<Animalwill be treated as an instance of Animal.

If you create some Cow instances and some Chicken instances and put in
the Farm<Animal>, and then loop through the contents of the farm, you
will get Animal references:

foreach (Animal animal in farm) { ... }

In the Farm<Tclass you can do anything to the animals that is defined
in the Animal class. The fact that the Animal class is abstract is not a
problem, as the Farm<Tclass is not allowed to create instances of T
anyway.

If you specify that T has to have a usable parameterless constructor,
then the Farm<Tis allowed to create instances of T, and you can only
use non-abstract classes as T:

public class Farm<T: IEnumerable<Twhere T : Animal, new() {...}

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: yaniv.golan | last post by:
Hi, Either I am missing something, or there is a compiler bug in the way inheritance from generic s is treated. In the following code, class c2 inherits from c1_generic. c1_generic has T as a...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.