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. C#
  4. polymorphism

polymorphism

Scheduled Pinned Locked Moved C#
oop
7 Posts 4 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.
  • N Offline
    N Offline
    netJP12L
    wrote on last edited by
    #1

    Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }

    A K 2 Replies Last reply
    0
    • N netJP12L

      Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      This is indeed a case of polymorphism based on the definition here.

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

      N 1 Reply Last reply
      0
      • A Abhinav S

        This is indeed a case of polymorphism based on the definition here.

        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

        N Offline
        N Offline
        netJP12L
        wrote on last edited by
        #3

        So, polymorphism process cannot happen without inheritance. Can it be done without it.

        E 1 Reply Last reply
        0
        • N netJP12L

          So, polymorphism process cannot happen without inheritance. Can it be done without it.

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Kind of, functional pointers (delegates in .NET) can achieve the same results as polymorphism in non-oo languages but by the strictest definition you need inheritance.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

          1 Reply Last reply
          0
          • N netJP12L

            Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            netJP12L wrote:

            ut does it mean changing a behaviour of a method in a drieved class also means polymorphism

            Yes but not necessarily: The base class's method is the one that is called until it is overridden in a sub class. If you need to define a method that you must change, the method must be abstract (as must the class in c#).

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

            N 1 Reply Last reply
            0
            • K Keith Barrow

              netJP12L wrote:

              ut does it mean changing a behaviour of a method in a drieved class also means polymorphism

              Yes but not necessarily: The base class's method is the one that is called until it is overridden in a sub class. If you need to define a method that you must change, the method must be abstract (as must the class in c#).

              Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

              N Offline
              N Offline
              netJP12L
              wrote on last edited by
              #6

              so why did the polymorphism includes the behaviour word in its definition. It only talks about types. I think behaviour and type is two different things.

              K 1 Reply Last reply
              0
              • N netJP12L

                so why did the polymorphism includes the behaviour word in its definition. It only talks about types. I think behaviour and type is two different things.

                K Offline
                K Offline
                Keith Barrow
                wrote on last edited by
                #7

                Behaviour and type are two things, otherwise we'd call them both behaviour or both type [edit] however, types exhibit behaviours. I think there has been some language based confusion here to clarify:

                class A
                {
                public virtual void Write()
                {
                Console.Writeline ("Base Write Method");
                }
                }

                class B : A
                {
                public override void Write()
                {
                Console.Writeline ("Class B's Write Method");
                }
                }

                class C : A
                {

                }

                class Foo
                {
                new A().Write(); // Outputs "Base Write Method"
                new B().Write(); // Outputs "Class B's Write Method" as base method is overridden
                new C().Write(); // Outputs "Base Write Method" as the base method isn't overridden
                }

                As per my original post, you can override the default behaviour, but it isn't necessary.

                Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                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