Tuesday, 24 January 2017

METHOD OVERRIDING VS METHOD HIDING AND METHOD OVERLOADING IN C# WITH EXAMPLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{

    public class BaseClass
    {
        public virtual void print()
        {
            Console.WriteLine("I am a  Base Class print Method ");
        }
    }
    public class DerivedClass : BaseClass
    {
/* public new void print() is for method hiding */ 
        public override void print()
        {
            Console.WriteLine("I am a Derived Class print Method ");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BaseClass B = new DerivedClass();
            B.print();
            Console.ReadLine();

        }

    }
}
======================
METHOD OVERLOADING
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication4;

namespace ConsoleApplication4
{
    // Method overloading , same method names but different parameters
    class Program
    {
        static void Main(string[] args)
        {
            Add(2, 3);  //N HERE BECAUSE OF STATIC METHOD BELOW WE CAN CALL THE                                     //METHOD WITHOUT INSTANCE 
            Console.ReadLine();
       
        }

        public static void Add(int FN, int LN)
        {
            Console.WriteLine("sum = {0} ", FN + LN);
        }

        public static void Add(int FN, int LN, out int SUM)
        {
            Console.WriteLine("I am a Derived Class print Method ");
            SUM = FN + LN;
        }

    }
}



No comments:

Post a Comment