Basic Concepts of OOP: Polymorphism

ZON
Posted by in OOPS category on for Beginner level | Points: 150 | Views : 99159 red flag
Rating: 4 out of 5  
 2 vote(s)

Basic Concepts of Object Oriented Programming : Polymorphism

This is based on Polymorphism,in which i am trying to make Polymorphism as easy as i can for those who don't have confidence on their Concepts..
Regards :
Software Engineer
Faizan Ahmed


 Download source code for Basic Concepts of OOP: Polymorphism

Introduction


In this article i m describing Polymorphism for those who want to make their concepts clear.


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 is done in OOP?

     
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___

public class fish
{
  public void eat()
   {
        console.writeline("fish eat");
    }
  public void swim()
   {
          console.writeline("fish swim");
    }
  }


______Derived Class______

class Dolphen:fish
{

  public void eat()
   {
        console.writeline("fish eat");
    }
  public void swim()
   {
          console.writeline("fish swim");
    }
}


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___

public class fish
{
  public void eat()
   {
        
console.writeline("fish eat");
    }
  public void swim()
   {
          
console.writeline("fish swim");
    }
  }


______Derived Class______

class Dolphin:fish
{

  public void eat()
   {
       
 console.writeline("Dolphin can eat");
    }
  public void swim()
   {
         
 console.writeline("Dolphin can swim");
    }
}


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______

class Dolphin : fish
        {

            public
 override void eat()
            {
                Console.WriteLine("Dolphin can eat");
            }
            public 
override void swim()
            {
                Console.WriteLine("Dolphin can swim");
            }
        }


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.

class Program
    {
        public class fish
        {
            public virtual void eat()
            {  }
            public virtual void swim()
            { }
            public virtual void dive()
            {}

        }
       public class Dolphin : fish
         {
            public override void eat()
            { Console.WriteLine("Dolphin eats Plants"); }
            public override  void swim()
            { Console.WriteLine("Dolphin swims quickly"); }
            public override  void dive()
            { Console.WriteLine("Dolphin dive deeply "); }
           public void dance()
            { Console.WriteLine("Dolphin can Dance"); }

        }
        public class Shark : fish
        {
            public override  void eat()
            { Console.WriteLine("Shark eats dead animal"); }
            public override  void swim()
            { Console.WriteLine("Sharks swim fastest than Dolphin"); }
            public override  void dive()
            { Console.WriteLine("Sharks Dive deeper than Dolphin"); }

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

        }
        
        static void Main(string[] args)
        {
            Dolphin D = new Dolphin();
            D.dance();
            D.dive();
            D.eat();
            D.swim();
            Shark S = new Shark();
            S.kill();
            S.dive();
            S.eat();
            S.swim();
            Console.ReadLine();
        }
    }


Conclusion


Hope this would help in understanding Polymorphism in OOP. Best of luck.




Page copy protected against web site content infringement by Copyscape

About the Author

ZON
Full Name: Faizan Ahmed
Member Level: Starter
Member Status: Member
Member Since: 8/31/2010 3:43:11 PM
Country: Australia
faizan_ahmed

A B.S graduate, seeking a challenging position in a progressive organization With an aim to contribute positively towards the objectives of The organization and to enhance my professional skills"

Login to vote for this post.

Comments or Responses

Posted by: Kazim6 on: 9/3/2010 | Points: 10
To remove this problem and to tell the compiler that which method is to be executed we need to use Polymorphism.
as you wrote this ... this concept is not right ...
well you can tackle this problem using new keyword ... if you try to excute without new keyword ... you will get warning nothing else ... the warning is that which i know .. :
your base class is hiding you must use new keyword ....

the basic concept of polymorphism is late binding which you dint teach us .. =)
never mind you are beginner keep it up

as you know me too i am kazim hussain 4th year SE from KU :p
Posted by: ZON on: 10/2/2010 | Points: 10
thank u kazim for responding me, yes i will teach u late binding as well but as i have mentioned here that i wrote this article for the beginers so i cant teach them late binding quick after polymorphism, i will teach u people more concepts then late binding would be taught, Thx :)
Posted by: Vikas0vanvi on: 12/15/2013 | Points: 25
can you please make tutorial on late binding , it nice tutorial please make more tutorial ,you are good in explaining this,thanks

Login to post response

Comment using Facebook(Author doesn't get notification)