Overriding in C#
Overriding in C# is the re-implementation of a base class method in a derived class. In this, the base class method is overridden in child class. The derived class method has the same name and signature as base class method. Overriding is useful in achieving Runtime polymorphism.
There are a few keywords that are used in method overriding.
1. Virtual – This keyword is used with a base class which signifies that the method of a base class can be overridden.
public virtual void Method() { // implementation }
2. Override – This keyword is used with a derived class which signifies that derived class overrides a method of a base class.
public override void Method() { // implementation }
3. Base – This keyword is used in a derived class to call the base class method.
public override void Method() { base.Method(); // implementation }
How Overriding Works in C#?
Below is an example of how we can implement overriding in C#.
class Parent { public virtual void Demo() // base class { Console.WriteLine(“This is parent”); } } class Child: Parent { public override void Demo() // derived class { Console.WriteLine(“This is child”); } }
In the above example there are two classes, one is base class or parent class and the other is derived class or we can say, child class. A base class method is derived in child class. In this, method in a parent is virtual which means it can be overridden by the child class. Override in a child means this method is the same as the parent class method with the same method signature.
Types of Overriding in C#
Below are the examples which show overriding with various keywords.
Example 1 – Without Virtual and Override Keywords
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Overriding { class Bird // base class { public void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public new void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { // main method static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } }
In the above example, no keyword is used in both bases as well as derived methods.
Also in main method, parent reference is used to call the child method. So in this case when no keyword is used, the parent method is called instead of a child method. So the output will be
Output :
Example 2 (a)- With Virtual and Override Keywords
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Overriding { class Bird // base class { public virtual void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public override void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { // main method static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } }
In this example, virtual is used in the base class which means it gives authority to child class to implement the method in its own way. In a derived class, override is used which means that the child method is the override method. Both the methods are the same with the same name and same method signature but the implementation part is different. In this example also, parent reference is used to call the child method. But as a parent is a method is virtual so the child method is called first instead of the parent method. So the output will be
Output :
Example 2 (b) – Virtual and Override Keywords
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Overriding { class Bird // base class { public virtual void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public override void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { //main method static void Main(string[] args) { Peacock p = new Peacock(); p.fly(); Console.ReadLine(); } } }
This example is the same as the previous example but this child, method is used for reference.
Output :
Example 3 – With Base Keyword
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Overriding { class Bird // base class { public virtual void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public override void fly() // derived class method { base.fly(); // base is use to call parent method Console.WriteLine("Peacock is flying"); } } class Program { static void Main(string[] args) { Peacock p = new Peacock(); p.fly(); Console.ReadLine(); } } }
In the above example, the base is used in a derived class to call the base class method. So in this base method is called first and then the derived method.
Output :
Example 4 – Abstract Classes with Overriding
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Overriding { abstract class Calculate { public abstract int sum(); } class Values : Calculate // derived class { int val1; int val2; public Values(int a = 0, int b = 0) { val1 = a; val2 = b; } public override int sum() { Console.WriteLine("sum of two values"); return (val1 + val2); } } class Program { static void Main(string[] args) { Values v = new Values(10, 20); int a = v.sum(); Console.WriteLine(a); Console.ReadLine(); } } }
In the above example, an abstract method is used. An abstract class is implemented by the derived class which contains an abstract method.
Output :
Rules for Method Overriding
- The method signature of a derived class should be the same as a base class.
- Overriding is not possible in the same class.
- Access modifiers must be the same for virtual methods and override methods.
- The virtual keyword is used in the base class method and Override is used in a derived class method.
- The base class method should not be static.
Conclusion
Overriding is useful in runtime polymorphism. It allows derived class to implement a base class method in its own way. So the method implementation is different of derived class from its base class. The overridden method can be virtual, override or abstract.
The above is the detailed content of Overriding in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
