Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    256
    Thanks
    3
    Thanked 11 Times in 11 Posts

    C# - How do I overwrite a parent method?

    Hey, anyone here familiar with C#?

    I have a parent child class much like this:

    Parent class
    Code:
    namespace zzz
    {
        public abstract class Parent
        {
            public Parent()
            {
                // Stuff
            }
    
            public string Print()
            {
                return "Words";
            }
        }
    }
    Child class
    Code:
    namespace zzz
    {
        public class Child : Parent
        {
            public Child() 
            {
                // Stuff
            }
    
            public string Print()
            {
                return "Different Words";
            }
        }
    }
    Main class
    Code:
    namespace zzz
    {
        public Testy
        {
            public static void main() 
            {
    	    Child name = new Child();
    
    	    Console.WriteLine(Child.Print() + "\n");
    	}
        }
    }
    The output of the writeline is "Words". How do I get it to output "Different Words?"
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...
    Get Firefox!

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Docs on override have a very basic example that should tip you in the right direction:
    http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

  • Users who have thanked Brandoe85 for this post:

    bobleny (07-16-2009)

  • #3
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    256
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Thanks! I just needed to "override" the method. I tried looking for a solution online but couldn't find anything. knowing that it is called overriding a method would have helped!

    Thanks again.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...
    Get Firefox!

  • #4
    New Coder
    Join Date
    Jul 2009
    Location
    Atlanta, GA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Methods you intend to override in a base class should be marked as virtual. You can also override non-virtual methods by using the "new" operator in the method signature of the derived class and calling the base class. Also notice how I called base() on the derived class' constructor. Here are two ways to override:

    Method 1
    Code:
      public abstract class Parent
      {
        public Parent()
        {
        }
        public virtual void Print()
        {
          //Do stuff
        }
      }
      public sealed class Child : Parent
      {
        public Child()
          : base()
        {
          
        }
        public override void Print()
        {
          base.Print();
        }
      }
    Method 2:
    Code:
      public abstract class Parent2
      {
        public Parent2()
        {
        }
        public void Print()
        {
          //Do stuff
        }
      }
      public sealed class Child2 : Parent2
      {
        public Child2()
          : base()
        {
    
        }
        public new void Print()
        {
          base.Print();
        }
      }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •