Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Inheritance vs Generics

Inheritance vs Generics

Scheduled Pinned Locked Moved .NET (Core and Framework)
visual-studiooopquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stryder_1
    wrote on last edited by
    #1

    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.

    N Z T P 4 Replies Last reply
    0
    • S Stryder_1

      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.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • S Stryder_1

        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.

        Z Offline
        Z Offline
        Zeeshan Amjad
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Stryder_1

          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.

          T Offline
          T Offline
          T M Gray
          wrote on last edited by
          #4

          These should give you a good start: An Introduction to C# Generics[^] The C# Station Tutorial Lesson 20: Introduction to Generic Collections[^]

          1 Reply Last reply
          0
          • N Not Active

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Looks like a <T> got eaten by the hamsters

            N 1 Reply Last reply
            0
            • L Lost User

              Looks like a <T> got eaten by the hamsters

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Guess Bob got hungry


              I know the language. I've read a book. - _Madmatt

              1 Reply Last reply
              0
              • S Stryder_1

                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.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Picture a bath. A conical bath. Film fine white sand running out of it. Now run the film backward. :cool:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups