Inheritance vs Generics
-
I've known about Generics for a while, but never really took the time to review. I've recently began taking a look at them and have been trying to determine when they should be used. It seems I can do the same things using inheritance. I was just wondering about everyone's opinion on this topic. Do you prefer one technique over the other and why? What scenario would a Generic be a better choice than an inherited class? Thanks.
-
I've known about Generics for a while, but never really took the time to review. I've recently began taking a look at them and have been trying to determine when they should be used. It seems I can do the same things using inheritance. I was just wondering about everyone's opinion on this topic. Do you prefer one technique over the other and why? What scenario would a Generic be a better choice than an inherited class? Thanks.
Generics and Inheritance are two completely different animals. Inheritance is of course used for OO where you can have a base class with some characteristics and actions that can be used in any derived classes. The classical example is and Animal base class and Cat or Dog derived classes. Generics give you the ability to write one class or method that can execute actions in a generic way. For instance;
public class MathOperations
{
public T Add<T>(T x, T y)
{
return x + y;
}
}In this case you can call Add with int, float, decimal, etc. and it will add the values together. Otherwise you would need to create an override for the Add method that takes, int, float, double, etc., much more coding.
I know the language. I've read a book. - _Madmatt
-
I've known about Generics for a while, but never really took the time to review. I've recently began taking a look at them and have been trying to determine when they should be used. It seems I can do the same things using inheritance. I was just wondering about everyone's opinion on this topic. Do you prefer one technique over the other and why? What scenario would a Generic be a better choice than an inherited class? Thanks.
Can't compare one with other. Inheritance is used in Object Oriented Programming and Generic is used in Generic Programming. One can't replace the advantage of one with other. Personally i perfer to use Generic for utility classes such as vector, array, map, list etc and inheritance for contract. What does it means, with the help of inheritance, i want to make sure that we uses the appropriate level of classes. Take a look at the following example
class Building
{
// do something
}class ResidentialBuilding : Building
{
// do something
}class RentedResidentialBuilding : ResidentialBuilding
{
// do someting
}class ApartmentBuilding : RentedResidentialBuilding
{
// do something
}class LuxaryApartmentBuilding : ApartmentBuilding
{
// do something
}Now if i want to give some functionality of Apartment then i would do something like this
void ApartmentName(const ApartmentBuilding& apartment);
This way i will make contract, with the help of inheritance, that only apartment and its inherited class can be used with this function. We can't do exactly the same with Generic. Similarly we can't do everything which generic offers with inheritance alone. The other answer already explained it.
-
I've known about Generics for a while, but never really took the time to review. I've recently began taking a look at them and have been trying to determine when they should be used. It seems I can do the same things using inheritance. I was just wondering about everyone's opinion on this topic. Do you prefer one technique over the other and why? What scenario would a Generic be a better choice than an inherited class? Thanks.
These should give you a good start: An Introduction to C# Generics[^] The C# Station Tutorial Lesson 20: Introduction to Generic Collections[^]
-
Generics and Inheritance are two completely different animals. Inheritance is of course used for OO where you can have a base class with some characteristics and actions that can be used in any derived classes. The classical example is and Animal base class and Cat or Dog derived classes. Generics give you the ability to write one class or method that can execute actions in a generic way. For instance;
public class MathOperations
{
public T Add<T>(T x, T y)
{
return x + y;
}
}In this case you can call Add with int, float, decimal, etc. and it will add the values together. Otherwise you would need to create an override for the Add method that takes, int, float, double, etc., much more coding.
I know the language. I've read a book. - _Madmatt
-
Guess Bob got hungry
I know the language. I've read a book. - _Madmatt
-
I've known about Generics for a while, but never really took the time to review. I've recently began taking a look at them and have been trying to determine when they should be used. It seems I can do the same things using inheritance. I was just wondering about everyone's opinion on this topic. Do you prefer one technique over the other and why? What scenario would a Generic be a better choice than an inherited class? Thanks.
Picture a bath. A conical bath. Film fine white sand running out of it. Now run the film backward. :cool: