Overriding in C#

PHPz
Release: 2024-09-03 15:13:28
Original
746 people have browsed it

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!

Related labels:
source:php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!