Method Overriding and Virtual Methods in c#

Method Overriding and Virtual Methods in c#

The method overriding concept in the C# programming language allows a derived class (user-defined subclass) to provide its own pre-defined implementation of a class function method already declared in the base root or parent class. A virtual method declared in the base class as a superclass allows the derived subclass to replace or extend the class’s default behavior.

Method Overriding and Virtual Methods in c#

Method overriding and virtual method concepts in C# programming.

Virtual method – A virtual method in a C# program is a method defined in the base class that can be overridden by a user-defined derived or subclass. A virtual method is declared using the virtual keyword in a C# program.

Override – A keyword used in a derived class in a C# program to provide a new implementation for a class method declared as virtual in a super or base class.

How method overriding works in C# programming.

  • It is used to override a user-defined class method in a C# program.
  • In this case, the class method in the super or base class must be indicated or marked with the virtual keyword. This indicates in the current program that there is a strong intent to override the method in a particular class.
  • The method override keyword is used to allow a derived or subclass connected to the base class to provide its own implementation of the method.
  • When a C# programmer calls an overridden class method in a program, the version in the existing derived class is executed, regardless of whether the C# programmer is using a reference to the super or base class type. This is a key element of the polymorphism feature in C# programming.

Example of class method overriding in C# programming.

To understand how the concept of method overriding in a super or main class behaves in C# programming, let’s understand it with an example.

Example of a base class with a virtual method in a C# program.

public class Course

{

// Here we define a virtual class method in the base class

public virtual void Select()

{

Console.WriteLine(“You can select your desired course.”);

}

}

Here’s an example of class method overriding in C#.

  • In this virtual method example, a virtual method is defined in the Select and Course classes. The base class provides a common implementation for Select, but allows derived subclasses to override it with their own implementations.

Example of a derived class with method overriding in C#.

public class Java : Course

{

// Here it overrides the Select method from the Course class

public override void Select()

{

Console.WriteLine(“You select a Java course.”);

}

}

public class Csharp : Course

{

// Here it overrides the Select method from the Course class

public override void Select()

{

Console.WriteLine(“You select a Csharp course.”);

}

}

Here, a derived class with method overriding example.

  • In the Java and Csharp classes, both classes override the Select() method. Each user-defined class provides a custom implementation, which is a special method for the Course class type.

Using Polymorphism in C# Programming.

So, let’s now learn how the polymorphism feature in C# programs helps us call overridden class methods through a base class reference.

public class Program

{

static void Main()

{

//Here we create an array of Course references

Course[] courses = new Course[3];

//Here we assign instances of Java and Csharp to the Course array

courses[0] = new Java();

courses[1] = new Csharp();

courses[2] = new Course(); // Here we use the Base class object

//Here we call the Select method on each course

foreach (var course in courses)

{

course.Select(); // Here we define Polymorphism in action

}

}

}

Polymorphism in C# Explanation.

In this program, courses is a grouped array of Course references, while the actual method executed is decided at runtime when the course.Select() class method is called. So the real object type (Java, Csharp, or Course) defined in the class invokes the correct version of the method depending on the class subclass.

This is a basic example of runtime polymorphism in a clear-cut C# program.

Detail Polymorphism Explanation in C#.

Using System;

// here we defiant a course Base/super class

public class courses

{

// here we define a Virtual method that can be overridden

public virtual void Select()

{

Console.WriteLine(“select your desire course.”);

}

}

// here we define a Derived class with name Java

public class Java: Course

{

// here we Override the Select method from the Course class

public override void Select()

{

Console.WriteLine(“You selected Java course.”);

}

}

// here we derived class Csharp

public class Csharp : Course

{

// here we Override the Select method from the Course class

public override void Select()

{

Console.WriteLine(“You selected Csharp course.”);

}

}

// Here we define a Main program

public class Program

{

static void Main()

{

// Here we create an array of Course references object

Course[] courses = new Course[3];

// Here we assign instances of Java and Csharp to the Course array

courses[0] = new Java();

courses[1] = new Csharp();

courses[2] = new Course(); // Here we create a Base class object

// Here we call the Select method on each course

foreach (var course in courses)

{

// Here we use the Polymorphism concept in action

course.Select();

}

}

}

When to use method overriding in a C# program.

  • Polymorphism – Method overriding in C# programs helps create more reliable, flexible, and maintainable program source code by allowing C# programmers to define or declare common behavior of base classes. While derived or subclasses within a base class can provide their own specialized implementations.
  • Extending or customizing behavior – If a C# programmer defines a common class method that should behave differently in derived or subclasses, method overriding is a useful concept for customizing class behavior within each subclass.

Calling a base method in an overridden class method in C# programming.

C# programmers can call the base class version of the method within an overridden class method by using the base keyword in the current class.

Example of calling a base method within an overridden class method.

public class Java : Course

{

public override void Select()

{

// here it calls the base class’s Select method

base.Select();

Console.WriteLine(“You select a Java course.”);

}

}

Here’s an example of calling a base method within an overridden class.

  • In this example, the Select class method in a Java class calls the base class version of Select using base.Select(). This allows C# programmers to maintain the base behavior and add additional functionality in derived classes.

Virtual Methods and Abstract Methods in C#.

  • Virtual class methods – In a C# program, methods can be overridden in a derived or subclass using the override keyword. While virtual methods declared in a class provide default behavior, they also allow for program customization.
  • Abstract class methods – Unlike virtual methods in a C# program, abstract methods declared in a program do not have an implementation in a base class. They must be implemented in a derived or subclass. In any C# program, an abstract class method is declared or defined with the abstract keyword.

Example of an abstract method with no implementation in the base class.

public abstract class Course

{

// Here we define an abstract class method with no implementation provided

public abstract void Select();

}

public class Java : Course

{

// Here we implement the abstract class method

public override void Select()

{

Console.WriteLine(“You select a Java course.”);

}

}

public class Program

{

static void Main()

{

Course java = new Java();

java.Select(); // Result is – You select a Java course

}

}

Here, in the case of an abstract method, there is no implementation in the base class.

In this program, the Select class method in the Course class is abstract, so the derived class (Java) must provide an implementation for it. C# programmers cannot directly create instances of abstract classes. However, they can create new instances of classes that implement its abstract methods.

Complete Example of an abstract method with no implementation in the base class.

Using System;

// here we use Abstract base class

public abstract class course

{

// here we use Abstract method with no implementation

public abstract void Select();

}

// here we define a Derived class Java

public class Java: Course

{

// here we implement the abstract class method

public override void Select()

{

Console.WriteLine(“You selected java course.”);

}

}

//here start a Main program class

public class program

{

static void Main()

{

//here we Create object using base class reference

Course java = new Java();

//here we Call the overridden class method

java.Select(); // Result is – You select java course.

}

}

Main Differences Between Virtual and Abstract Methods

FeatureVirtual Class MethodAbstract Class Method
Where to ImplementVirtual class method Can have an implementation in the base class.Abstract class method Does not have an implementation in the base class.
Where to OverridingVirtual method Can be overridden in derived or subclasses using the override reserved keyword.Abstract class method Must be overridden in derived subclasses.
How to InstantiationVirtual method Can be used in an instantiated base class.Abstract class method Base class cannot be instantiated (it is abstract) method behaviour.
Default work BehaviorVirtual method Provides default class behavior.Abstract class method has No default behavior is provided.

Summary of Method Overriding and Virtual Methods in C#.

  • Virtual Class Method – In C# programming, a method is defined in a base class. It can be overridden in a derived or subclass to provide special behavior. This is marked or indicated by the virtual keyword in the base or superclass.
  • Overridden Method – In C# programming, an overridden method is used in a derived or subclass to provide a new implementation of a base class method. The override keyword in a C# program is used to modify a virtual method from a base class in a derived class.
  • Polymorphism Features – Method overriding in a C# program allows calling a derived or subclass method even when you use a base class reference for the class object. This is a detailed explanation of runtime polymorphism in the current program.
  • Calling a Base Method – C# programmers can call a base class method from a derived class method by using the base keyword.
  • Abstract Class Method – Abstract class methods in a C# program do not have an implementation in the base class, and must be implemented in a derived class.

Leave a Reply