Polymorphism: Method Overloading and Method Overriding and Method Hiding

using System;
using Polymorphism;

namespace Polymorphism
{
    public static class CalCc
    {
        public static int add(int a, int b)
        {
            return a + b;
        }

        public static int add(int a, int b, int c)
        {
            return a + b + c;
        }


        public static float add(float a, float b)
        {
            return (a + b);
        }
    }
}
    

    class ProgramTest
    {
        static void Main(string[] args)
        {
        Console.WriteLine(CalCc.add(12,23));
        Console.WriteLine(CalCc.add(12,23,25));
            Console.WriteLine(CalCc.add(5.6f,7.8f));
            Console.ReadKey();
        }
    }

using System;
using Polymorphism;

namespace Polymorphism
{
    public static class CalCc
    {
        public static int add(int a, int b)
        {
            return a + b;
        }

        public static int add(int a, int b, int c)
        {
            return a + b + c;
        }


        public static float add(float a, float b)
        {
            return (a + b);
        }
    }
}
    

    class ProgramTest
    {
        static void Main(string[] args)
        {
        Console.WriteLine(CalCc.add(12,23));
        Console.WriteLine(CalCc.add(12,23,25));
            Console.WriteLine(CalCc.add(5.6f,7.8f));
            Console.ReadKey();
        }
    }

Method Overriding:

//override mane ektar opor arekta chore bosha
//mane ekta method er opor arekta method chore boshakei bole method overriding
//eta runtime ei hoy
//jei method tay override hoy setay override use korte hoy
//jei method tar opor override hoy seake virtual sombodhon korte hoy
//in this example we are overriding the eat method
using System;

namespace MethodOverride
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog d=new Dog();
            d.eat();
            Console.ReadKey();
        }
    }

    public class Animal
    {
        public virtual void eat()
        {
            Console.WriteLine("Eating Food.");
        }
    }


    public class Dog : Animal
    {
        public override void eat()
        {
            Console.WriteLine("Eating bread");
        }
        
    }

}

another good exmple:

using System;

namespace MethodOverrideExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Cars[] c = new Cars[2];
            c[0]=new newCars();
            c[1]=new oldCars();

            foreach (Cars key in c)
            {
                key.printInfo();
            }
            Console.ReadKey();

        }
    }
    
    class Cars
    {
        public virtual void printInfo()
        {
            Console.WriteLine("This is the base class");
        }
    }

    class newCars : Cars
    {
        public  override void printInfo()
        {
            Console.WriteLine("This is new cars class");
        }

    }

    class oldCars : Cars
    {

        public override void printInfo()
        {
            Console.WriteLine("This is the old cars");
        }
       
    }


}

this video is good for learning overriding:

Method Hiding:

using System;


namespace Method_Hiding
{
    class FC
    {
        public virtual void Display()
        {
            Console.WriteLine("FC: Display");
        }
    }

    class SC : FC
    {
        public new void Display()
        {
            //base.Display();
            Console.WriteLine("SC: Display");
        }
    }

    class TC : SC
    {
        public new void Display()
        {
            //base.Display();
            Console.WriteLine("TC: Display");
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            SC p1=new SC();
            p1.Display();

            //((FC) p1).Display(); oobject casting

            FC p2=new FC();
            p2.Display();


            // SC pp = new FC(); it is not possible
            Console.ReadKey();

        }
    }
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *