IT SOLUTIONS
916-726-5675
   Error 404: Page not found. Try browsing or searching.   
-Collapse +Expand

C#

Search C# Group:

Advanced
-Collapse +Expand C# Group Home
-Collapse +Expand Message Board
-Collapse +Expand Knowledge Base
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE
-Collapse +Expand Members Only
Prestwood Tip Jar
Tip Jar
Finding something useful?

Add to the
Tip Jar!

Prestwood eMagazine

Subscribe now!
Enter your email:


   Prestwood ITPrestwoodBoardsKBProgrammingC#OOP   
Go To Random Article
  From the March 2010 Issue of Prestwood eMag
 
C# OOP:
C# Overriding (virtual, override)
By Mike Prestwood
10/28/2008, Last updated 3/8/2009
 
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page
 Tags: C# , Overriding , , Programming, C#, OOP


General Info: Method Overriding

Where you define or implement a virtual method in a parent class and then replace it in a descendant class.

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.

In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.

C# Overriding

In C#, you specify a virtual method with the virtual keyword in a parent class and extend (or replace) it in a descendant class using the override keyword.

Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().

Syntax Example:
class Robot
{
  public virtual void Speak()
  {
  }
}

class Cyborg:Robot
{
  public override void Speak()
  {
  }
}

Override Details

  • You cannot override a regular non-virtual method, nor a static method.
  • The first version of the parent method must be virtual or abstract.
  • You can override any parent method marked virtual, abstract, or override (already overridden).
  • The methods must have the same signature.
  • The methods must have the same visibility (the same access level).
  • Use the base keyword to refer to the parent class as in base.SomeMethod().

C# Override Example

The following code snippet demonstrates using virtual and override to override a parent method in a descendant class.

using System;

class Dog
{
    public virtual void Bark()
    {
          Console.WriteLine("RUFF!");
    }
}

class GermanShepard:Dog
{
    public override void Bark()
    {
          Console.WriteLine("Rrrrooouuff!!");
    }
}

class Chiuaua:Dog
{
    public override void Bark()
    {
          Console.WriteLine("ruff");
    }
}

class InclusionExample
{
    public static void Main()
    {
         Dog MyDog=new Dog();

         MyDog=new GermanShepard();
MyDog.Bark(); // prints Rrrrooouuff!!

MyDog=new Chiuaua();
         MyDog.Bark();  // prints ruff;
    }
}

Hiding a Method with New

Use the new keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using new but you will get a compiler warning. Using new will suppress the warning.

The new and override modifiers have different meanings. The new modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.

Avoid Introducing New Members: Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate.

class Robot : System.Object
{
  public void Speak()
  {
    MessageBox.Show("Robot says hi");
  }
}
  
class Cyborg : Robot
{
  new public void Speak()
  {
    MessageBox.Show("hi");
  }
}

Calling the Base Class Version

A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the base keyword to refer to the parent class as in base.SomeMethod().

class Robot : System.Object
{
public virtual void Speak()
{
MessageBox.Show("Robot says hi");
}
}
  
class Cyborg : Robot
{
public override void Speak()
{
base.Speak();
MessageBox.Show("hi");
}
}

 

More Info

Definition:  Method Overriding
Code Contributed By Prestwood staff member Mike Prestwood:
mprestwood
Email Approved! E CA USA

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.


Comments

0 Comments.
Would you like to comment? Reply? Ask a question? Say thanks?
Add Comment



 KB Article #101472 Counter
3450
Since 10/28/2008

Sponsored Ad
Brought to you by Prestwood IT Solutions
We hope you are enjoying our knowledge base! We welcome your participation in our open online community. As the caretakers, we pay our staff to moderate, edit, and contribute free content here in an effort to promote our software development company. Keep us in mind if you or your company needs help. Whether you need a single developer or a team, consider Prestwood. We are an American company that keeps jobs in America (no off shoring and no need for H-1B developers). Our hope is that you will put some or all of your development tasks in our hands. We specialize in hourly help (1 hour minimum), custom websites from $2k to $40k, business database applications from $5k to $60k, and enterprise application development starting from $20k. Talented Developer?
If you are a talented developer and would like to work with us, start by filling out our Register for Work form.
 
Mike Prestwood
Need service or help?
Have a question? Contact Us.
--Mike Prestwood
Follow us on: 
366 People Online Now!!  
Online Now: Sign In to see who's online now!  Not a member? Join Prestwood now. It's free!
1995-2010 Prestwood IT Solutions.   [Security & Privacy]   Made in the U.S.A..   No H1-B.   No offshoring.