Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Ok I know what polymorphism is in theory.
What I would like to understand is for example;

Public Class DerivedClass
      Inherits BaseClass

      Public Function Do() As BaseClass
        'Code to do something and return a value
      End Function
End Class


I know that my example is not very expansive, but I am looking to try and understand is how this version of polymorphism works, does it (depending on the code writen within the function) return the correct derived class. Can you give me a simple working example on how it works.

Thank You in advance!
Posted

To generalize on Jpuckett's example....

Polymorphism is a kind of hierarchic classification system. To use biology as an example, you can create a class LivingThing that encodes all the characteristics of living things: it eats, it reproduces, it adapts. You then create classes Plant and Animal which inherit from LivingThing. Both plants and animals eat, reproduce and adapt, but they do so differently; Plant.Eat() and Animal.Eat() would therefore be implemented differently.

Likewise, Animal is the base class for Reptile and and Mammal. Both types of animals eat the same, so both could use the base Animal.Eat() method. They have very different ways of reproducing, though and so would have different Replicate() methods. Along with the different implementations (Reptile uses the Egg framework while Mammal uses Placenta), Reptile.Replicate() would return an object of type Reptile while Mammal.Replicate() would return an object of type Mammal

Ultimately, you can call Eat() and Replicate() on any object ultimately derived from LivingThing. As far as the user/coder is concerned, they are the same methods regardless of the parent class, even though the internal implementation is not. Same method, different forms... polymorphism.
 
Share this answer
 
v3
Comments
Saumitra Kumar Paul 3-Jun-11 16:13pm    
Your "LivingThing" example have helped me to conceptualize clearly the "Class". For a long time, i could not understand what a class exactly is. Thank you very much.
EDIT: In direct relation to

I know that my example is not very expansive, but I am looking to try and understand is how this version of polymorphism works, does it (depending on the code writen within the function) return the correct derived class. Can you give me a simple working example on how it works.

You can write a method anywhere that returns a type like what you have.

Public Function Do() As BaseClass
End Function

This function doesn't have to exist in the derived class at all. All this does is return an object. This would return the object of type BaseClass just like a Function FUBAR() As Int32 would return an integer.
END EDIT



Here's a nice example that I think I saw on MSDN a couple years back.

Every cat in the world is a Cat. It's also an animal. It's also a living thing. But not every cat is the same. Take the following example.


public class Cat
    {
        public string Genus { get; set; }
        public string Species { get; set; }
        public void Eat(object something)
        {
            //Eat something.
        }
        public void Sleep(int hours)
        {
            //Sleep is essential
        }
        public void BodilyFunctions()
        {
            //All cats do this
        }
    }

    public class Tiger : Cat
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public Tiger()
        {
            Genus = "Panthera";
            Species = "tigris";
        }

        public void GoHunting()
        {
            Eat("Human");
            Sleep(9);
            BodilyFunctions();
        }

        public void BeLazyBecauseItsHotInIndia()
        {
            Sleep(12);
        }
    }

    public class HouseCat : Cat
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public HouseCat()
        {
            Genus = "Felis";
            Species = "catus";
        }

        public void EatFromTheBowl()
        {
            Eat("CatFood");
        }

        public void EverydayLife()
        {
            Purr();
            EatFromTheBowl();
            Purr();
            BeLazy();
            Purr();
        }

        public void BeLazy()
        {
            Sleep(12);
        }

        public void Purr()
        {
            //Make purring noise
        }
    }


House cats don't eat people, but both house cats and tigers have a genus and a species. Now technically I should be using an Interface here because both House Cat and Tiger (be-verb-form of is) are Cats, but they also both "have" a genus and species, so this applies.

What does this mean? It means that your base class (which in this case SHOULD be abstract, but that's another course of discussion and study), gets morphed and extended into other things by changing it's initial setup. However, in .NET there is only single inheritance, so you must be mindful that when you inherit a class, you inherit the entirety of that class, and that can change the way you look at code.

An ASP.NET MVC commonality (a real-world example) is to use Controllers for particular sections of a site, say a secured section that requires authentication. You can just as easily put a decorator on each of your controller methods, or you can implement a "BaseController" that all controllers inherit from, and check there. The school of thought you might see out there for something like this is DRY -- don't repeat yourself.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900