C# Program to Demonstrate the Overriding
Posted by Superadmin on August 13 2022 11:13:05

C# Program to Demonstrate the Overriding

 

 

This is a C# Program to demonstrate the concept of overriding.

Problem Description

This C# Program Demonstrates the concept of Overriding.

Problem Solution

Here the override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Program/Source Code

Here is source code of the C# Program to Demonstrate the concept of Overriding. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate the concept of Overriding
 */
using System;
abstract class ShapesClass
{
    abstract public double Area();
}
class Circle : ShapesClass
{
    int radius = 0;
    public Circle(int n)
    {
        radius = n;
    }
    public override double Area()
    {
        return 3.14 * radius * radius;
    }
    static void Main()
    {
        int r;
        Console.WriteLine("Enter the Radius :");
        r = int.Parse(Console.ReadLine());
        Circle c1 = new Circle(r);
        Console.WriteLine("Area of the Circle = {0}", c1.Area());
        Console.ReadLine();
    }
}
Program Explanation

In this C# Program, we are reading the radius of the circle using ‘r’ variable. Perform the operations of Circle() function by passing ‘r’ variable value as an argument. The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

 
Runtime Test Cases
 
Enter the Radius : 3
Area of the Circle : 28.26