Introduction

We can create different type of properties with different type of modifiers.

Consider the following code.

The Area property inside Shape is defined as virtual. The subclass can override it.

Demo

using System;

class Shape/*w  w  w .  ja va2s  .  com*/
{
    public virtual double Area
    {
        get
        {
            return 0;
        }
    }
}
class Circle : Shape
{
    int radius;
    public Circle(int radius)
    {
        this.radius = radius;

    }
    public int Radius
    {
        get
        {
            return radius;
        }
    }
    public override double Area
    {
        get
        {
            return 3.14 * radius * radius;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {

        Circle myCircle = new Circle(10);
        Console.WriteLine("\nRadius of the Cricle is {0} Unit", myCircle.Radius);
        Console.WriteLine("Area of the Circle is {0} sq.Unit", myCircle.Area);
    }
}

Result

Related Topic