hi, i created a webpage explaining what polymorphism is. could someone point to me any inaccuracies or additions that i could make?

Polymorphism


Syntax

Polymorphism is when one type can take the form of a related type. Here is an example:
DerivedClass B = new DerivedClass();
B.DoWork();
BaseClass A = (BaseClass)B; // class B is a derived class and therefore, casting is needed.
A.DoWork();


Explanation

In the explanation section, i will use inheritance and overriding methods to demonstrate to you the concept of polymorphism. The reason i will use inheritance is because inheritance
has got one base class and several derived classes that are associated with it (thus making polymorphism valid as one class can take the form of another).


Short explaination on overriding methods

As it was explained in the inheritance section Inheritance, there is a base class that holds fields/methods and every derived class gets them.
Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed. To achieve that a method in the derived class
needs to override the virtual method in the base class. here is an example:
This code demonstrates polymorphism and overriding of methods:

class Program
    {
        static void Main(string[] args) // The goal in the code is to create one base object and many derived objects (the animals) and make each object implement its own mehtod.

        {
            Animal[] zoo = new Animal[6];
            zoo[0] = new Monkey();
            zoo[1] = new Dog();
            zoo[2] = new Ez();
            zoo[3] = new Person();
            zoo[4] = new Cow();
            zoo[5] = new Cat(); // Foooooooooooooooo
            for (int i = 0; i < zoo.Length; i++)
            {
                zoo[i].MakeNoise(); // This is how polymorphism manifests itself. MakeNoise in every derived class should override the method in the base class.
            }
            Console.WriteLine("======");  // Notice the  method invocations below because an explanation will follow.

            Cat cat = new Cat();
            cat.MakeNoise();// Cat Miyau
            Console.WriteLine("==parsi===");
           
            Animal pCat = new ParsiCat();
            pCat.MakeNoise(); //Foooooooooooooo

            Cat pcat1 = new ParsiCat();
            pcat1.MakeNoise();//Cat Parsi: Miyauuu

        }
    }
}
class Animal
{
    public virtual void MakeNoise()  // you need to use the word virtual to accept an overriding from the derived class.
    {
        Console.WriteLine("Foooooooooooooooo");
    }
}
class Dog : Animal
{
    public override void MakeNoise()  // the word override must be there to override the method. if you dont put that word, "new" word will be set as a default, which means, no overriding!!
    {
        Console.WriteLine("Dog: Wooff");
    }
}
class Ez : Animal
{
    public override void MakeNoise()
    {
        Console.WriteLine("Meehעעhee");
    }
}
class Cow:Animal
{
    public override void MakeNoise()
    {
        Console.WriteLine("MOOOOOoooooo....");
    } 
}
class Monkey : Animal
{
    public override void MakeNoise()
    {
        Console.WriteLine("ahahahaoobovosssoso");
    }
}
class Person : Animal
{
    public override void MakeNoise()
    {
        Console.WriteLine("BoynaLehMipo");
    }
    public new void MakeNoise()
    {
        Console.WriteLine("Caaaaaaaaaaaaaaaaaaaaaaaaaaaasting");
    }
}
class Cat:Animal
{
    public new virtual void MakeNoise()  // To make a new virtual method set the words "new virtual".
    {
        Console.WriteLine("Cat: Miyauuu");
    }
}
class ParsiCat:Cat
{
    public override void MakeNoise()  // ParsiCat will override the method that is inside the Cat class
    {
        Console.WriteLine("Cat Parsi: Miyauuu");
    }

Note: you can reach a derived classes method even without the override word by casting: ((Person)Animal[3]).Foo(); (Output = Caaaaaaaaaaaaaaaaaaaaaaaaaaaasting). For more information on casting see .............. A derived class can stop virtual inheritance by declaring an override as sealed. This requires putting the sealed keyword before the override keyword in the class member declaration.

public class C : B
{
    public sealed override void DoWork() { }
}


Accessing Base Class Virtual Members from Derived Classes:
A derived class that has replaced or overridden a method or property can still access the method or property on the base class using the base keyword.

public class Base
{
    public virtual void DoWork() {/*...*/ }
}
public class Derived : Base
{
    public override void DoWork()
    {
        //Perform Derived's work here
        //...
        // Call DoWork on base class
        base.DoWork();
    }
}

For those of you who want to know another trick using base and inheritance, see the following example:
In this example i try to print the area of each shape.

class Program
    {
        static void Main(string[] args)
        {

            Line line = new Line(500);
            line.PrintLine();


            Rec rec = new Rec(25, 25);
            rec.PrintRec();

         
            Cube cube=new Cube(10,5,6);
            cube.PrintCube(); //300

            
        }
    }
}
class Line
{
    int x;
    public Line(int x)
    {
        this.x=x;
    }
    public int X
    {
        get
        {
            return x;
        }
    }
    public void PrintLine()
    {
        Console.WriteLine(X);
    }
}
class Rec : Line
{
    int y;
    public Rec(int x,int y):base(x)
    {
        this.y = y;
    }
    public int Y
    {
        get
        {
            return y;
        }
    }
    public void PrintRec()
    {
        Console.WriteLine(X*Y);
    }
}
class Cube:Rec
{
    int z;
    public Cube(int x, int y, int z):base(x,y)
    {
        this.z = z;
    }
    public int Z
    {
        get
        {
            return z;
        }
    }
    public void PrintCube()
    {
        Console.WriteLine(X * Y * Z);
    }

Lets see what happens! lets say i send parameters to Cube. This is the process that happens:

The base(x,y) contructor of Cube class gets them first-----> it sends them to Rec(its base class)-----> Rec has got another base(x) constructor, it sends x to Line---->

Line gets x and sets it---> then all the derived classes obtain x (including cube)--->same process happens with y, once it is being obtained it is set and passed to Cube--->

---> cube now has got all the variables to calculate and print the area.

What happens is that values go up to the base class and then go down to each of the derived classes according to the hierarchy!!!

Recommended Answers

All 2 Replies

Line 9: Casting is not needed to go from a derived class to a base class. It is needed to go from a base class to a derived class.

ooh, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.