Wednesday, June 15, 2011

Learning Overloading and Overriding in C#

So let us start with overloading.
What is overloading? overloading is a feature of the object-oriented concepts called "Polymorphism". It has the ability to create multiple functions/methods or properties with the exact name but different parameters. To make it more clearer, take a look at my example.

using System;


namespace ConsoleApplicationForBlog
{
    class Program 
    {
        int firstNumber;
        int secondNumber;
        
        //
        // Overloading
        //
        private int Add(int x, int y)
        {
            return (x + y);
        }


        private string Add(string x, string y)
        {
            return (x + y);
        }


        static void Main(string[] args)
        {
            Program program = new Program();
            // For integer
            int result;
            
            program.firstNumber = 10;
            program.secondNumber = 15;


            result = program.Add(program.firstNumber, program.secondNumber);
            Console.WriteLine(result);
            
            // For string
            string fullName;


            fullName = program.Add("Eric John", " Adamos");
            Console.WriteLine(fullName);
            Console.ReadKey();
        }
    }
}


The result is:
25                        
Eric John Adamos  

Overriding is another feature of polymorphism in which such cases that you would want to change the functionality of that method for another class without changing its base functionality. In this case, we can override the property of the base class. Take a look at my code:


using System;

namespace OverridingConsoleBlog
{
    // This is your base class.
    public class Animal
    {
        public virtual void display()
        {
            Console.WriteLine("Animal!");
        }
    }

    // This is a class named "Dog" which inherits "Animal".
    public class Dog : Animal
    {
        // Note: the name of your method should be
        // the same on your base class.
        public override void display()
        {
            // This overrides your base class display().
            Console.WriteLine("Hi! I'm a dog.");
        }
    }

    public class Cat : Animal
    {
        // Note: the name of your method should be
        // the same on your base class.
        public override void display()
        {
            // This overrides your base class display().
            Console.WriteLine("Hi! I'm a cat.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Animal();
            Animal dog = new Dog(); // or Dog dog = new Dog();
            Animal cat = new Cat(); // or Cat cat = new Cat();

            animal.display(); 
            dog.display();
            cat.display();

            Console.ReadKey();
        }
    }
}

Result :
Animal!           
Hi! I'm a dog.  
Hi! I'm a cat.   

Have fun! :)

No comments:

Post a Comment