Whats wrong with this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zacksoniar
    New Member
    • Sep 2007
    • 45

    Whats wrong with this code

    Hi all,

    Code:
    namespace ConsoleApplication1
    {
        class I
        {
            public void PrintIt()
            {
                PrintOne();
                
                ((I)this).PrintOne();
            }
    
            public virtual void PrintOne()
            {
                Console.WriteLine("I am from base.\n");
            }
        }
    
        class G:I
        {
            public override void PrintOne()
            {
                Console.WriteLine("I am from sub.\n");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                G g = new G();
                g.PrintIt();
                Console.ReadLine();
            }
        }
    }
    I am expecting the output to be

    "I am from sub"

    "I am from base"

    but its not coming.... where am i making mistake?

    I know I could achieve this by calling base.PrintOne in subclass... but i dont want it that way.

    I want it to be called from base itself.

    Please comment.

    Thanks in advance.
    Zacksoniar
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    That is the way polymorphism works. You override the method. And you declare the
    Code:
    G g = new G();
    so you basically after cast you will still have something like
    Code:
    I i = new G();
    More answers here http://stackoverflow.com/questions/4...n-method-c-net

    Comment

    • zacksoniar
      New Member
      • Sep 2007
      • 45

      #3
      thanks Monomachus...

      but even in this case.. I am not getting what i want i.e.
      "I am from sub"
      "I am from base"


      I know I should have called base.PrintOne() from overridden method.
      But I dont want to do that way. I want to achieve calling of base class method & sub class method from base class function only.

      My code compiles .. doesn't it mean it is possible?

      with ur approach too .. i get
      "I am from sub"
      "I am from sub"


      It is just to understand polymorphism & upcasting..

      Thnaks

      Comment

      • Monomachus
        Recognized Expert New Member
        • Apr 2008
        • 127

        #4
        It is not possible. What I meant is that after casting your I object is still instantiated as a G, so he'll get the PrintOne() method from the G because the I one is virtual, meaning what is said here http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

        "When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member."

        So your G even casted will still point to a G and when the I virtual method is invoked, the run-time type of your object is G. And so it will call PrintOne() from G.

        The bottom line is when you instantiate your object as a G, using new G(); it won't change its run-time type.

        Hope this helps.

        Comment

        • zacksoniar
          New Member
          • Sep 2007
          • 45

          #5
          then what is the significance of this line
          Code:
          I i = new G();
          does it mean nothing in C#, cause in java above statement mean a lot of things..

          thanks in advance

          Comment

          • rekedtechie
            New Member
            • Feb 2012
            • 51

            #6
            class I contains the virtual void PrintOne()

            then you override the virtual void PrintOne() in class G

            you need to call the class that contains your virtual void PrintOne() then assign the class that contains override..

            I i = new G();
            i.PrintOne();
            //you get the override value of the virtual method

            I i = new I();
            i.PrintOne();
            //you get the value of the virtual method

            G g = new G();
            g.PrintOne();
            //you get nothing, because you wish to call for override without any virtual method.

            Comment

            • rekedtechie
              New Member
              • Feb 2012
              • 51

              #7
              let say in real life..

              .

              class I, virtual void PrintOne() - is a delivery man that has delivery box.

              .

              class G, override void PrintOne() - is another delivery delivery box.

              .

              delivery man can deliver the delivery box.

              .

              delivery man can deliver another delivery box.

              .

              but another delivery box cant deliver itself, it needs a delivery man. :))

              Comment

              • zacksoniar
                New Member
                • Sep 2007
                • 45

                #8
                Thanks everybody .. :-)

                Comment

                Working...