C#.Net - Method Overriding
Implementation in C#.NET
- C# (pronounced "C-sharp") is a high-level, modern, object-oriented programming language developed by Microsoft.
- C# is a key language in the Microsoft .NET ecosystem
- It is commonly used for a wide range of application development, including web applications, desktop applications, mobile apps, cloud services.
- C# is widely used in enterprise application development, game development (via Unity), web development (in ASP.NET Core and Blazor)
To implement method overriding in C#.NET, the programmer needs to follow these steps:
- Ensure that the derived class inherits from the base class.
- In C#.NET, : symbol to implement inheritance.
- In the above example, D inherits from B, and it overrides the m1() in the derived class.
- The method m1() in class B must be defined with the virtual keyword.
class B
{
public virtual void m1()
{
// Base class implementation
}
}
class D : B
{
public override void m1()
{
// Custom implementation in the derived class
}
}
The class diagram shown above is implemented in C#.NET. The Person class is defined as follows:
using System;
namespace ConsoleApp1
{
internal class Person
{
//fields to store First and Last Name of a person
//Declares as protected to make it accessible in Employeee class
protected string firstName;
protected string lastName;
//Constructor to initiliaze fields
public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
//Displays name in Firstname and Lastname format
//Can be overridden in sub class
public virtual void completeName()
{
Console.WriteLine(this.firstName + " " + this.lastName);
}
public void show()
{
Console.WriteLine("Person");
}
}
The Employee class is defined by inheriting from the Person class. The constructor is also defined to pass parameters required by Person class constructor. Java provides base keyword to pass parameters from Employee class(Sub class) to the Person class(Super class) from the constructor of the sub class.
The completeName() method gets inherited from the Person class. It displays the name of the Person in the First and Last name format. It is marked with the virtual keyword to allow Overriding.
Method Overriding is required when name of the Employee is required in the Last and First name format. It is implemented by redefining completeName() method in the Employee class with the same signature and with the override keyword as demonstrated in the code below:
using System;
namespace ConsoleApp1
{
internal class Employee : Person
{
//It is compulsory to define constructor in Sub class
//As no default constructor in super class
//Its job is provide parameters required by super class constructor
public Employee(string firstName, string lastName) : base(firstName, lastName)
{
//C#.Net provides base keyword to pass parameters to super class constructor
}
//Method inherited from Person is overridden here
//Displays name in Lastname and Firstname format
public override void completeName()
{
//base.completeName();
Console.WriteLine(this.lastName + " " + this.firstName);
}
}
}
Once the Person and the Employee classes are defined then can be tested using the following code.
//code to test the defined Person and Employee Class
using System;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
//Creating an instance or an object
//A reference variable of Super class can refer to
// Objects of Super as well as Sub Class
Person person; //Reference of Person Class
//Refers to object of Person Class
person = new Employee("Neeraj", "Chopra");
person.completeName();
//Refers to object of Employee Class
person = new Employee("Neeraj", "Chopra");
person.completeName();
Console.ReadKey();
}
}
}
The output of the above code is:
Chopra NeerajChopra Neeraj
Wondering:
- Why all that lengthy code?
- Why not make change in Person class code to get the work done?
Remember in a typical development Scenario, a team of developers working who may be from different organizations. One developer has developed the Person class and shared .dll file (Dynamic Link Library) with other developers. Source code is not shared therefore code is not available to update it.
What if both the versions of the completeName() is required?
Method Overriding is feature of Object-Oriented Languages to achieve runtime polymorphism. Recall in When Person(Super class) Object refers to the object of the Person class calls the super class version of the completeName() method and when it refers to the object of the Employee class (Sub class) calls the sub class version of the completeName() method.
However, the developer of Person may decide not to allows the users of his/her class to override than the developer will not mark completeName() method with the virtual keyword.
C#.Net - Method Overriding
Reviewed by Syed Hafiz Choudhary
on
September 17, 2023
Rating:
No comments: