Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Get Started Developing for Android Apps with Eclipse Articles
Serial Number in SSRS Articles
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
How to Print a Crystal Report direct to printer Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
Html5 interview questions and answers Interview Questions
SharePoint 2010 interview Questions Answers Interview Questions
Infosys Interview Questions Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
Populate or bind Dropdownlist in Asp.net using Jquery and Json Articles
ASP.NET MVC Overview Interview Questions
Submit Articles | More Articles..

Basic Concepts Polymorphism

Posted By: Jaishree On:9/20/2010 2:45:25 AM in:Articles Category:OOPS Hits:1651
In this article i m describing basics of Polymorphism

Polymorphism

The word Polymorphism means of many forms.In programming this word is meant to reuse the single code multiple times. In object oriented programming its a big question that why the Polymorphism is done, what is the purpose of it in our code?

There are lots of people who don't even know the purpose and usage of Polymorphism.Polymorphism is the 3rd main pillar of OOP without it the object oriented programming is incomplete. Lets go in the depth of Polymorphism.

Why Polymorphism


Its obvious that when we do inheritance between two classes, all the methods and properties of the first class are derived to the other class so that this becomes the child class which  adobes all the functionality of base class.It can also possesses its own separate methods.

But there is a big problem in inheriting the second class to the first class as it adobes all the methods same as the base class has,which  means that after inheritance both(base class& child class) have the methods of same name and same body as shown in this example:

Base Class

  1. public class fish
  2. {
  3.   public void eat()
  4.    {
  5.         console.writeline("fish eat");
  6.     }
  7.   public void swim()
  8.    {
  9.           console.writeline("fish swim");
  10.     }
  11.   }


Derived Class

  1. class Dolphin:fish
  2. {

  3.   public void eat()
  4.    {
  5.         console.writeline("fish eat");
  6.     }
  7.   public void swim()
  8.    {
  9.           console.writeline("fish swim");
  10.     }
  11. }

In this example it is clear that when we Inherit two classes, all the methods with the same name and same body are adobed by the derived class as shown above.Both methods have the same name but if we change the body of the second method then it makes Compiler disturb whether to compile base class method or derived class's
method first... 

Lets have a look of this code..

Base Class

  1. public class fish
  2. {
  3.   public void eat()
  4.    {
  5.         console.writeline("fish eat");
  6.     }
  7.   public void swim()
  8.    {
  9.           console.writeline("fish swim");
  10.     }
  11.   }


Derived Class

  1. class Dolphin:fish
  2. {

  3.   public void eat()
  4.    {
  5.         console.writeline("Dolphin can eat");
  6.     }
  7.   public void swim()
  8.    {
  9.           console.writeline("Dolphin can swim");
  10.     }
  11. }


In this example we have changed the body of the methods of Derived class.Now this will make the Compiler disturb to compile which method first,because they both have same name but different bodies.

To remove this problem and to tell the compiler that which method is to be executed we need to use Polymorphism.

How the Polymorphism is done?


Polymorphism is used to remove the problem which is shown in the above code.It is done not by changing the name of the methods of both base & derived class.Infect it is done by adding the "virtual" keyword before the base class method, and the "override" keyword before the derived class method.As shown in this exmple:
 
Base Class
       
public class fish
        {
            public virtual void eat()
            {
                Console.WriteLine("fish eat");
            }
            public virtual void swim()
            {
                Console.WriteLine("fish swim");
            }
        }    
 

Derived Class

  1. class Dolphin : fish
  2.         {

  3.             public override void eat()
  4.             {
  5.                 Console.WriteLine("Dolphin can eat");
  6.             }
  7.             public override void swim()
  8.             {
  9.                 Console.WriteLine("Dolphin can swim");
  10.             }
  11.         }


This is actually the Polymorphism in which we write virtual keyword with the base class method and we write override keyword with the derived class method as we did. It helps the compiler to select the method to be executed.

Here is the complete code in which Polymorphism has been applied.

  1. class Program
  2.     {
  3.         public class fish
  4.         {
  5.             public virtual void eat()
  6.             {  }
  7.             public virtual void swim()
  8.             { }
  9.             public virtual void dive()
  10.             {}

  11.         }
  12.        public class Dolphin : fish
  13.          {
  14.             public override void eat()
  15.             { Console.WriteLine("Dolphin eats Plants"); }
  16.             public override  void swim()
  17.             { Console.WriteLine("Dolphin swims quickly"); }
  18.             public override  void dive()
  19.             { Console.WriteLine("Dolphin dive deeply "); }
  20.            public void dance()
  21.             { Console.WriteLine("Dolphin can Dance"); }

  22.         }
  23.         public class Shark : fish
  24.         {
  25.             public override  void eat()
  26.             { Console.WriteLine("Shark eats dead animal"); }
  27.             public override  void swim()
  28.             { Console.WriteLine("Sharks swim fastest than Dolphin"); }
  29.             public override  void dive()
  30.             { Console.WriteLine("Sharks Dive deeper than Dolphin"); }

  31.             public void kill()
  32.             { Console.WriteLine("Shark kills Others"); }

  33.         }
  34.         
  35.         static void Main(string[] args)
  36.         {
  37.             Dolphin D = new Dolphin();
  38.             D.dance();
  39.             D.dive();
  40.             D.eat();
  41.             D.swim();
  42.             Shark S = new Shark();
  43.             S.kill();
  44.             S.dive();
  45.             S.eat();
  46.             S.swim();
  47.             Console.ReadLine();
  48.         }
  49.     }

comments powered by Disqus
User Profile
Jaishree
Sr. Engineer
Delhi , India
Email :You must Log In to access the contact details.
Latest Post from :Jaishree
Function to Convert Text String to Title Case – Proper Case
View: 903 | Submitted on: 3/6/2013 11:23:44 PM
Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application
View: 2003 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1320 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1186 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1288 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles | All Post of This User..

All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher