8
Polymorphism (Virtual, Override, New,) In a base class if we declare a method as virtual and then in its derived classes we have the same method which has been overridden then if we call that method we don’t get the base class one and we get the one in that derived class. Consider the following code: public class mycar { public virtual void describeMe() { Console.WriteLine("I am a car"); } } public class benz : mycar { public override void describeMe() { Console.WriteLine("And I am a benz"); } } public class porche : mycar { public override void describeMe() { Console.WriteLine("i am a porche"); } } Now if in the main method we have: class testPolymorphism { static void Main(string[] args) { mycar c = new mycar(); benz b = new benz(); porche p = new porche();

PolyMorphism Virtual Override New

Embed Size (px)

Citation preview

Page 1: PolyMorphism Virtual Override New

Polymorphism (Virtual, Override, New,)

In a base class if we declare a method as virtual and then in its derived classes we have the same method which has been overridden then if we call that method we don’t get the base class one and we get the one in that derived class.

Consider the following code:public class mycar{ public virtual void describeMe() { Console.WriteLine("I am a car"); }

}

public class benz : mycar{ public override void describeMe() { Console.WriteLine("And I am a benz"); }

}

public class porche : mycar{ public override void describeMe() { Console.WriteLine("i am a porche"); }

}

Now if in the main method we have: class testPolymorphism { static void Main(string[] args) { mycar c = new mycar(); benz b = new benz(); porche p = new porche();

c.describeMe(); b.describeMe(); p.describeMe();

Console.WriteLine("\n-------------------------"); Console.WriteLine("-------------------------\n");

mycar[] cars = new mycar[3];

Page 2: PolyMorphism Virtual Override New

cars[0] = new mycar(); cars[1] = new benz(); cars[2] = new porche();

foreach (mycar mc in cars) { mc.newTest(); }

Console.WriteLine("\n-------------------------"); Console.WriteLine("-------------------------\n");

((mycar)cars[0]).describeMe(); ((benz)cars[1]).describeMe(); ((porche)cars[2]).describeMe();

Console.Read();

} }

Then we get the following result:

So either we have our objects declared in an array of the base class type: mycar[] cars = new mycar[3]; cars[0] = new mycar(); cars[1] = new benz(); cars[2] = new porche();

or declared them explicitly: mycar c = new mycar(); benz b = new benz(); porche p = new porche();

Page 3: PolyMorphism Virtual Override New

every time that we call the overridden method we get the method in that class not the base class. So if we say: c.describeMe(); b.describeMe(); p.describeMe();or: foreach (mycar mc in cars) { mc.describeMe(); }We get the same result.

But

If we have a method in the base class and we declare the same method in its derived classes with new keyword in the derived classes we get the version in the derived classes only if we explicitly declare them (1): mycar c = new mycar(); benz b = new benz(); porche p = new porche();

Not if they are in an array of the base class type (2): mycar[] cars = new mycar[3]; cars[0] = new mycar(); cars[1] = new benz(); cars[2] = new porche();

And if we want to get the method in the derived classes when they are elements of an array of a base class type then we have to explicitly cast it to the derived classes.So if we say: b.describeMe();

we get the decribeMe() in benz class but if we say: cars[1].describeMe();

We still get the describeMe() method in mycar class not the one in benz class.To be able to get describeMe() in benz we have to say: ((benz)cars[1]).describeMe();

So if we have the following classes:public class mycar{ public virtual void describeMe() { Console.WriteLine("I am a car"); }

public void newTest() { Console.WriteLine("I am in new:car"); }

}

Page 4: PolyMorphism Virtual Override New

public class benz : mycar{ public override void describeMe() { Console.WriteLine("And I am a benz"); }

public new void newTest() { Console.WriteLine("I am in new:benz"); }

}

public class porche : mycar{ public override void describeMe() { Console.WriteLine("i am a porche"); }

public new void newTest() { Console.WriteLine("I am in new:porche"); }

}And the following main: class testPolymorphism { static void Main(string[] args) { mycar c = new mycar(); benz b = new benz(); porche p = new porche();

c.newTest(); b.newTest(); p.newTest();

Console.WriteLine("\n-------------------------"); Console.WriteLine("-------------------------\n");

mycar[] cars = new mycar[3]; cars[0] = new mycar(); cars[1] = new benz(); cars[2] = new porche();

foreach (mycar mc in cars) { mc.newTest();

Page 5: PolyMorphism Virtual Override New

}

Console.WriteLine("\n-------------------------"); Console.WriteLine("-------------------------\n"); ((mycar)cars[0]).newTest(); ((benz)cars[1]).newTest(); ((porche)cars[2]).newTest();

Console.Read();

} }

The output will be:

We can have new and virtual together in scenarios like below:class Car{ public virtual void DescribeCar() { System.Console.WriteLine("Just a car."); }}

class ConvertibleCar : Car{ public new virtual void DescribeCar() { System.Console.WriteLine("a convertable car."); }}

class Minivan : Car

Page 6: PolyMorphism Virtual Override New

{ public override void DescribeCar() { System.Console.WriteLine("a minivan car."); }}So if it is our main: class testPolymorphism { static void Main(string[] args) {

Car c1 = new Car(); ConvertibleCar cc = new ConvertibleCar(); Minivan mv = new Minivan();

c1.DescribeCar(); cc.DescribeCar(); mv.DescribeCar();

Console.WriteLine("\n-------------------------"); Console.WriteLine("-------------------------\n");

Car[] myC = new Car[3];

myC[0] = new Car(); myC[1] = new ConvertibleCar(); myC[2] = new Minivan(); foreach( Car cm in myC ) { cm.DescribeCar(); }

Console.Read();

} }

The output will be as shown below:

Page 7: PolyMorphism Virtual Override New

SoWhen we have new key word we have to either declare the class implicitly or if it is an array of the base class we have to use casting.

Good luck