IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the November 2015 Issue of Prestwood eMag
 
C# OOP:
C# Overriding (virtual, override)
 
Posted 16 years ago on 10/28/2008 and 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

KB101472

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

Comments

1 Comments.
Share a thought or comment...
First Comment
Comment 1 of 5

Thank you to clear my concept of overriding.

---
jatin arora
Posted 13 years ago

Comment 2 of 5

C# overriding, Virtual and override these kind of things where mention in given blog. So i am trying to leave answers which just come to take best essay help. Not all the time you start to define in these given pages, Not all time you understand these type of blogs but it is little bit easy to undersd these blogs.

Posted 55 months ago

Comment 3 of 5

The masonry restoration has been a great feature for most of us. 

Posted 55 months ago

Comment 4 of 5

This was kilometer zero and you understand about which we are talking here. Actually, i am showing interest to have the perfect portland printers who tell the way where they do work and ever have to understand the same thing.

Posted 45 months ago

Latest Comment
Comment 5 of 5

Cables are also sale on different websites. These websites are made for only product salling purpose when you come and make them means you first have to add them in cart and then public speaking coach it. After paying amount through you bank account or some other account which they accepted.

Posted 45 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P146A1
Enter key:
Code Contributed By Mike Prestwood:

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.

Visit Profile

 KB Article #101472 Counter
29221
Since 10/28/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]