Table of Contents
How Overriding Works in C#?
Types of Overriding in C#
Example 1 – Without Virtual and Override Keywords
Example 2 (a)- With Virtual and Override Keywords
Example 2 (b) – Virtual and Override Keywords
Example 3 – With Base Keyword
Example 4 – Abstract Classes with Overriding
Rules for Method Overriding
Conclusion

Overriding in C#

Sep 03, 2024 pm 03:13 PM
c# c# tutorial

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
}
Copy after login

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
}
Copy after login

3. Base – This keyword is used in a derived class to call the base class method.

public override void Method()
{
base.Method();
// implementation
}
Copy after login

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”);
}
}
Copy after login

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();
}
}
}
Copy after login

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 :

Overriding in C#

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();
}
}
}
Copy after login

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 :

Overriding in C#

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();
}
}
}
Copy after login

This example is the same as the previous example but this child, method is used for reference.

Output : 

Overriding in C#

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();
}
}
}
Copy after login

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 :

Overriding in C#

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();
}
}
}
Copy after login

In the above example, an abstract method is used. An abstract class is implemented by the derived class which contains an abstract method.

Output : 

Overriding in C#

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Active Directory with C#

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Access Modifiers in C#

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Random Number Generator in C#

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

C# Data Grid View

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Patterns in C#

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

C# StringReader

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

C# StringWriter

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

BinaryWriter in C#

See all articles