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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Polymorphism and inheritance

Polymorphism and inheritance

Scheduled Pinned Locked Moved C#
oopcsharpc++
7 Posts 3 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.
  • G Offline
    G Offline
    Goncalo Oliveira
    wrote on last edited by
    #1

    Let's see if I can explain my small doubt. I can go around this, but as I was coding something, I got curious if something wasn't possible to do in c#. In C++, you could have something like this: class A { public virtual void method() { // something } } class AA : public A { public void method() { // something else } } class AB : public B { public void method() { // even something else } } A *obj; if ( something ) { obj = (A)( new AA() ); } else { obj = (A)( new AB() ); } obj->method(); What happens here is that when you invoke the method function, you'll be executing the implementation from AA or from AB and not from A. Though, in C#, if I try to do something like this, you'll be executing the implementation from A. That fact to me, is a shame... I liked this behavior. And as pointers in C# only exist for values, I suppose this is not possible with C#. Best regards

    Gonçalo A.

    O 1 Reply Last reply
    0
    • G Goncalo Oliveira

      Let's see if I can explain my small doubt. I can go around this, but as I was coding something, I got curious if something wasn't possible to do in c#. In C++, you could have something like this: class A { public virtual void method() { // something } } class AA : public A { public void method() { // something else } } class AB : public B { public void method() { // even something else } } A *obj; if ( something ) { obj = (A)( new AA() ); } else { obj = (A)( new AB() ); } obj->method(); What happens here is that when you invoke the method function, you'll be executing the implementation from AA or from AB and not from A. Though, in C#, if I try to do something like this, you'll be executing the implementation from A. That fact to me, is a shame... I liked this behavior. And as pointers in C# only exist for values, I suppose this is not possible with C#. Best regards

      Gonçalo A.

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      Why dont you try it and see what happens?

      using System;
      using System.Collections.Generic;
      using System.Text;

      namespace VirtualMethods
      {
      class Program
      {
      static void Main(string[] args)
      {
      A a = new A();
      A aa = new AA();
      A ab = new AB();
      A ac = new AC();

              Console.WriteLine(a.GetValue());
              Console.WriteLine(aa.GetValue());
              Console.WriteLine(ab.GetValue());
              Console.WriteLine(ac.GetValue());
      
              Console.ReadLine();
          }
      }
      

      }

      class A
      {
      public virtual string GetValue()
      {
      return "A";
      }
      }

      class AA : A
      {
      public override string GetValue()
      {
      return "AA";
      }
      }

      class AB : A
      {
      public override string GetValue()
      {
      return "AB";
      }
      }

      class AC : A
      {
      public override string GetValue()
      {
      return base.GetValue();
      }
      }

      G 1 Reply Last reply
      0
      • O originSH

        Why dont you try it and see what happens?

        using System;
        using System.Collections.Generic;
        using System.Text;

        namespace VirtualMethods
        {
        class Program
        {
        static void Main(string[] args)
        {
        A a = new A();
        A aa = new AA();
        A ab = new AB();
        A ac = new AC();

                Console.WriteLine(a.GetValue());
                Console.WriteLine(aa.GetValue());
                Console.WriteLine(ab.GetValue());
                Console.WriteLine(ac.GetValue());
        
                Console.ReadLine();
            }
        }
        

        }

        class A
        {
        public virtual string GetValue()
        {
        return "A";
        }
        }

        class AA : A
        {
        public override string GetValue()
        {
        return "AA";
        }
        }

        class AB : A
        {
        public override string GetValue()
        {
        return "AB";
        }
        }

        class AC : A
        {
        public override string GetValue()
        {
        return base.GetValue();
        }
        }

        G Offline
        G Offline
        Goncalo Oliveira
        wrote on last edited by
        #3

        override.... right. I feel like a newbie, but I guess these things happen when moving on to a new language :p Thank you for the quick reply, you helped a lot. :)

        Gonçalo A.

        O 1 Reply Last reply
        0
        • G Goncalo Oliveira

          override.... right. I feel like a newbie, but I guess these things happen when moving on to a new language :p Thank you for the quick reply, you helped a lot. :)

          Gonçalo A.

          O Offline
          O Offline
          originSH
          wrote on last edited by
          #4

          no problem :) You can only override a virtual or abstract method, but you can "new" a method over any old one. i.e. class AA : A { public new string GetValue() { return "NEW"; } } But this will behave how you originally described ... if cast as 'A' it'll call the original method on the A class. It'll only call the 'new' method if cast as an AA.

          G 1 Reply Last reply
          0
          • O originSH

            no problem :) You can only override a virtual or abstract method, but you can "new" a method over any old one. i.e. class AA : A { public new string GetValue() { return "NEW"; } } But this will behave how you originally described ... if cast as 'A' it'll call the original method on the A class. It'll only call the 'new' method if cast as an AA.

            G Offline
            G Offline
            Goncalo Oliveira
            wrote on last edited by
            #5

            Yes, that was how I was doing it. The original was a virtual method, but I expected him to override automatically. Though, it does make more sense this way, and offers more possibilities. Thank you, once again, for the explanation.

            Gonçalo A.

            L 1 Reply Last reply
            0
            • G Goncalo Oliveira

              Yes, that was how I was doing it. The original was a virtual method, but I expected him to override automatically. Though, it does make more sense this way, and offers more possibilities. Thank you, once again, for the explanation.

              Gonçalo A.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, you should pay attention to the messages the compiler emits. If you try and redefine a virtual method without either "new" or "override", a warning is issued. Don't lower the warning level (a project setting, 4 is a good value) to get rid of (some of) the messages, study the messages and adapt your code instead. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Happy 2008!


              G 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, you should pay attention to the messages the compiler emits. If you try and redefine a virtual method without either "new" or "override", a warning is issued. Don't lower the warning level (a project setting, 4 is a good value) to get rid of (some of) the messages, study the messages and adapt your code instead. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Happy 2008!


                G Offline
                G Offline
                Goncalo Oliveira
                wrote on last edited by
                #7

                Yes, I noticed that already. Thanks.

                Gonçalo A.

                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