What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10678 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Polymorphism-An OverView ...
Sabarimahesh

Polymorphism-An OverView

 Blog author: Sabarimahesh | Posted on: 5/9/2012 | Category: C# Blogs | Views: 694 | Status: [Member] | Points: 75 | Alert Moderator   


Introduction


Through this topic I'm going to share about polymorphism.It is a powerful 

concept in OOPS. I believe that this will be very much helpful for beginners. 

Polymorphism

  • To Invoke Method Of Derived Class through Base Class during Runtime.
  • To Provide Different implementations of methods that are called through the same name.
  • There are Two Types Of Polymorphism

1)Compile Time Polymorphism

2)Run Time Polymorphism


Compile Time Polymorphism


  • It can be called as Method Overloading. 
  • This Method has Same Name with different Arguments.
  • Use of Method Overloading

1)To eliminate complexity

2)To Increase performance

Example

class Program  
{    
        static void Main(string[] args)
        {
            Overloaded("Sabari");
            Overloaded("Sabari","Mahesh");
            Overloaded("Sabari","Mahesh ", "P M");
          
            Console.ReadLine();
        }
 
        static void Overloaded(string value)
        {          
                Console.WriteLine(value);          
        }
        static void Overloaded(string value, string value1)
        {
            Console.WriteLine(value+value1);
        }
        static void Overloaded(string value, string value1, string value2)
        {
            Console.WriteLine(value+ value1 + value2)
        }      
}


Run Time Polymorphism


  • It Can Be Called as Method Overriding.
  • You Can't Override  Non-Virtual  or Static Methods.
  • You Can Override  if the BaseClass Method is virtual, abstract or Override.
  • Method overriding is possible only in derived classes, but not within the same class.
  • Use of Method Overriding

1)To Modify Methods

2)To Modify Properties

3)To Modify an Indexes

4)To Modify an Event

Example 1:


Class BaseClass consists of function Show().

Class DerivedClass hides the function Show()

class Program
    {             static void Main(string[] args)
        {
         Baseclass bc;
         bc = new Baseclass();
         bc.Show();
         Console.ReadLine();
 
        }
  
    }
 
    class Baseclass
    {
        public void Show()
        {
            System.Console.WriteLine("Baseclass::Show");
        }
    }
 
    class DerivedClass : Baseclass
    {
        new public void Show()
        {
            System.Console.WriteLine("DerivedClass::Show");
        }
    }
 

Output

Baseclass::Show

Show() of class BaseClass is executed because the reference variable b refers to the object of class BaseClass.



Example 2

Creating an object of Derived class DerivedClass and storing its reference in the reference variable bc of typeBaseClass. This is valid in C#.

class Program
    {             static void Main(string[] args)
        {
         Baseclass bc;
         bc = new Baseclass();
         bc.Show();
         bc = new DerivedClass();
         bc.Show();
         Console.ReadLine();
 
        }
  
    }
 
    class Baseclass
    {
        public void Show()
        {
            System.Console.WriteLine("Baseclass::Show");
        }
    }
 
    class DerivedClass : Baseclass
    {
        new public void Show()
        {
            System.Console.WriteLine("DerivedClass::Show");
        }
    }

Output

Baseclass::Show

Baseclass::Show 

bc is a reference of type Baseclass, the function Show() of class Baseclass will be invoked, no matter whom bc refers to. 

 

Example 3


class Program
    {    
        static void Main(string[] args)
        {
         Baseclass bc;
         bc = new Baseclass();
         bc.Show();
         bc = new DerivedClass();
         bc.Show();
         bc = new DerivedClass2();
         bc.Show();  
         Console.ReadLine();
 
        }
  
    }
 
    class Baseclass
    {
        public void Show()
        {
            System.Console.WriteLine("Baseclass::Show");
        }
    }
 
    class DerivedClass : Baseclass
    {
        new public void Show()
        {
            System.Console.WriteLine("DerivedClass::Show");
        }
    }
 
    class DerivedClass2 : Baseclass
    {
        new public void Show()
        {
            System.Console.WriteLine("DerivedClass2::Show");
        }
    }

Output

Baseclass::Show

Baseclass::Show

Baseclass::Show

it invokes the functions of the class that matches its Reference bc, 
bc contains the reference to a particular derived class object, then its supposed to invoke the function of that class

Example 4

class Program
    {             static void Main(string[] args)
        {
         Baseclass bc;
         bc = new Baseclass();
         bc.Show();
         bc = new DerivedClass();
         bc.Show();
         bc = new DerivedClass2();
         bc.Show();  
         Console.ReadLine();
 
        }
  
    }
 
    class Baseclass
    {
        public virtual void Show()
        {
            System.Console.WriteLine("Baseclass::Show");
        }
    }
 
    class DerivedClass : Baseclass
    {
        public override void Show()
        {
            System.Console.WriteLine("DerivedClass::Show");
        }
    }
 
    class DerivedClass2 : DerivedClass
    {
        public override void Show()
        {
            System.Console.WriteLine("DerivedClass2::Show");
        }
    }

Output


Baseclass::Show

DerivedClass::Show

DerivedClass2::Show

The function Show() of Base class Baseclass is declared as virtual, while the implementation of Show() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable Baseclass and invoke Show(). The rite versions of Show get invoked based on the object the reference variable refers to.


    Example 5


    class Program
        {             static void Main(string[] args)
            {
             Baseclass bc;
             bc = new DerivedClass2();
             bc.Show();  
             Console.ReadLine();
     
            }
      
        }
     
        class Baseclass
        {
            public virtual void Show()
            {
                System.Console.WriteLine("Baseclass::Show");
            }
        }
     
        class DerivedClass : Baseclass
        {
            public override void Show()
            {
                System.Console.WriteLine("DerivedClass::Show");
            }
        }
     
        class DerivedClass2 : DerivedClass
        {
            public new void Show()
            {
                System.Console.WriteLine("DerivedClass2::Show");
            }
    }

    Output 

    DerivedClass::Show

    We Know that DerivedClass2 was called.
    But, The Method inside the Class (DerivedClass2will not be displayed; because it is hidden. Also, we didn't override the method


    Example 6

    We Have 5 Classes: One Baseclass and Four DerivedClasses 

    The BaseClass have a Virtual method 

    The First Derived class Have a method with "new" Key word , 

    i.e;Data of this class will be hidden. 

    This is the Major purpose Of Using New Keyword. 

    We Override All The other Derived classes except the First Derived class.

    Note: 
    
    You Can't Use This:
    public new void Show()
    DerivedClass2.Show() cannot override inherited member DerivedClass.Show() because it is not marked virtual, abstract, or override

    You Can Use this
    public virtual new void Show()
    Because this is also a Virtual Method

    class Program
        {
            static void Main(string[] args)
            {
                Baseclass bc;
                bc = new DerivedClass4();
                bc.Show();
                Console.ReadLine();
     
            }
     
        }
     
        class Baseclass
        {
            public virtual void Show()
            {
                System.Console.WriteLine("");
            }
        }
     
        class DerivedClass : Baseclass
        {
            public virtual new void Show()
            {
                System.Console.WriteLine("DerivedClass::Show");
            }
        }
        class DerivedClass2 : DerivedClass
        {
            public override void Show()
            {
                System.Console.WriteLine("DerivedClass2::Show");
            }
        }
        class DerivedClass3 : DerivedClass2
        {
            public override void Show()
            {
                System.Console.WriteLine("DerivedClass3::Show");
            }
        }
        class DerivedClass4 : DerivedClass3
        {
            public override void Show()
            {
                System.Console.WriteLine("DerivedClass4::Show");
            }
    }

    Output

    Baseclass::Show

    Baseclass::Show


    Conclusion


    We have learned about polymorphism. I hope that my article was helpful. 




    Life is a Race
    Thanks & Regards
    By
    Sabari Mahesh P M
    Found interesting? Add this to:


    Experience:1 year(s)
    Home page:http://www.dotnetfunda.com
    Member since:Thursday, March 15, 2012
    Level:Bronze
    Status: [Member]
    Biography:C#,ASP.NET, JAVA SCRIPT,SQL, J QUERY,MVC 3

     Responses

    Funda
    Posted by: Funda | Posted on: 5/11/2012 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

    Hi Boss,
    Nice and Simple Blog....

    Regards
    Funda...

    >> Write Response - Respond to this post and get points

    More Blogs

    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 9:17:38 PM