top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Virtual Methods in C#.NET with example

0 votes
162 views

When you want to allow a derived class to override a method of the base class, within the base class method must be created as virtual method and within the derived class method must be created using the keyword override.

When a method declared as virtual in base class, then that method can be defined in base class and it is optional for the derived class to override that method. 

When it needs same definition as base class, then no need to override the method and if it needs different definition than provided by base class then it must override the method.

Method overriding also provides more than one form for a method. Hence it is also an example for polymorphism.


The following example creates three classes shape, circle and rectangle where circle and rectangle are inherited from the class shape and overrides the methods Area() and Circumference() that are declared as virtual in Shape class.

 

using System;

namespace ProgramCall
{
    class Shape
    {

        protected float R, L, B;
        public virtual float Area()
        {

            return 3.14F * R * R;
        }

        public virtual float Circumference()
        {
            return 2 * 3.14F * R;
        }

    }

    class Rectangle : Shape
    {
        public void GetLB()
        {
            Console.Write("Enter  Length  :  ");

            L = float.Parse(Console.ReadLine()); 

            Console.Write("Enter Breadth : ");
            B = float.Parse(Console.ReadLine());
        }
        public override float Area()
        {

            return L * B;
        }

        public override float Circumference()
        {
            return 2 * (L + B);
        }
    }

    class Circle : Shape
    {
        public void GetRadius()
        {
            Console.Write("Enter  Radius  :  ");

            R = float.Parse(Console.ReadLine());
        }
    }

    class MainClass
    {
        static void Main()
        {

            Rectangle R = new Rectangle();
            R.GetLB();

            Console.WriteLine("Area : {0}", R.Area()); 

            Console.WriteLine("Circumference : {0}", R.Circumference());

            Console.WriteLine();

            Circle C = new Circle(); 
            C.GetRadius();

            Console.WriteLine("Area : {0}", C.Area());
            Console.WriteLine("Circumference : {0}", C.Circumference());

            Console.Read();
        }
    }
}


Output
 

Enter  Length  :  10
Enter Breadth : 20
Area : 200
Circumference : 60

Enter  Radius  :  25
Area : 1962.5
Circumference : 157

posted Feb 10, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

The purpose of abstract class is to provide default functionality to its sub classes.

When a method is declared as abstract in the base class then every derived class of that class must provide its own definition for that method.

 An abstract class can also contain methods with complete implementation, besides abstract methods.

When a class contains at least one abstract method, then the class must be declared as abstract class

It is mandatory to override abstract method in the derived class.

When a class is declared as abstract class, then it is not possible to create an instance for that class. But it can be used as a parameter in a method.
 

Abstract class in c# example



The following example creates three classes shape, circle and rectangle where circle and rectangle are inherited from the class shape and overrides the methods Area() and Circumference() that are declared as abstract in Shape class and as Shape class contains abstract methods it is declared as abstract class.

 

using System;

namespace ProgramCall
{

    //Abstract class
    abstract class Shape1
    {

        protected float R, L, B;

        //Abstract methods can have only declarations
        public abstract float Area();
        public abstract float Circumference();

    }


    class Rectangle1 : Shape1
    {
        public void GetLB()
        {
            Console.Write("Enter  Length  :  ");

            L = float.Parse(Console.ReadLine()); 

            Console.Write("Enter Breadth : ");

            B = float.Parse(Console.ReadLine());
        }

        
        public override float Area()
        {
            return L * B;
        }

        public override float Circumference()
        {
            return 2 * (L + B);
        }

    }


    class Circle1 : Shape1
    {

        public void GetRadius()
        {

            Console.Write("Enter  Radius  :  ");
            R = float.Parse(Console.ReadLine());
        }

        public override float Area()
        {
            return 3.14F * R * R;
        }
        public override float Circumference()
        {
            return 2 * 3.14F * R;

        }
    }
    class MainClass
    {
        public static void Calculate(Shape1 S)
        {

            Console.WriteLine("Area : {0}", S.Area());
            Console.WriteLine("Circumference : {0}", S.Circumference());

        }
        static void Main()
        {

            Rectangle1 R = new Rectangle1(); 
            R.GetLB();
            Calculate(R);

            Console.WriteLine();

            Circle1 C = new Circle1(); 
            C.GetRadius();
            Calculate(C);

            Console.Read();

        }
    }
}


Output
 

Enter  Length  :  10
Enter Breadth : 12
Area : 120
Circumference : 44

Enter  Radius  :  5
Area : 78.5
Circumference : 31.4

Note

In the above example method calculate takes a parameter of type Shape1 from which rectangle1 and circle1 classes are inherited.
 
A base class type parameter can take derived class object as an argument. Hence the calculate method can take either rectangle1 or circle1 object as argument and the actual argument in the parameter S will be determined only at runtime and hence this example is an example for runtime polymorphism.

READ MORE

Sealed Classes

When you want to restrict your classes from being inherited by others you can create the class as sealed class. 

To create a class as sealed class, create the class using the keyword sealed.
 

[Access Modifier] sealed class classname

{

}
 

Sealed Methods 

The virtual nature of a method persists for any number of levels of inheritance. 

For example there is a class “A” that contains a virtual method “M1”. Another class “B” is inherited from “A” and another class “C” is inherited from “B”. In this situation class “C” can override the method “M1” regardless of whether class “B” overrides the method “M1”. 

At any level of inheritance, if you want to restrict next level of derived classes from overriding a virtual method, then create the method using the keyword sealed along with the keyword override.
 

[Access Modifier] sealed override returntype methodname([Params])

{

}

READ MORE
...