Overloading And OverRiding in C#

This blog represents OverLoading And OverRiding functions.

We have Class A

class A

{       

   virtual public void add(int a)

   {
        Console.WriteLine(a);

       
//Console.WriteLine("pavan");

   }

   virtual public void add(int a, int b)

   {

         Console.WriteLine(a+b);

   }

   virtual public void add(int a, int b, int c)

   { 

        Console.WriteLine(a + b + c);

   }            

}

We inherited Class A

class B:A

{
    override public void add(int x)

    {

        Console.WriteLine(x);

    }

    override public void add(int i, int j)

    {

        Console.WriteLine(i + j);

   }

   override public void add(int a, int b, int c)

   {

       Console.WriteLine(a + b + (c=0));

   }
 

Class B is inherited to Class C

class C:B

{
    override public void add(int c)

    {

        Console.WriteLine(c);

    }

    override public void add(int a, int b)

    {

        Console.WriteLine(a + b);

       
//Console.WriteLine("basya");

    }

    override public void add(int a, int b, int c)

    {

        Console.WriteLine(a + b + c);

    }

}

 

In MAIN function

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            A a = new A();

            a.add(5);

            a.add(4, 0);

            a.add(3, 5, 7);


            B b = new B();

            b.add(9);

            b.add(2, 4);

            b.add(1, 2, 4);

 

            C c = new C();

            c.add(9);

            c.add(8, 6);

            c.add(12, 34, 12);

            Console.ReadKey();

        }

    }

}  

Following snapshot shows the output…

Image-1.jpg

Ebook Download
View all
Learn
View all