I.T. Discussion Community!
-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

April 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 11 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...
Comment 1 of 1

Thank you to clear my concept of overriding.

---
jatin arora
Posted 8 years 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 = P1244A1
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
25485
Since 10/28/2008
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech