Polymorphism Virtual Override New

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Polymorphism Virtual Override New as PDF for free.

More details

  • Words: 751
  • Pages: 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(); c.describeMe(); b.describeMe(); p.describeMe();

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

cars = new mycar[3]; = new mycar(); = new benz(); = 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[0] cars[1] cars[2]

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

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

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[0] cars[1] cars[2]

cars = new mycar[3]; = new mycar(); = new benz(); = 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"); } } 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[0] cars[1] cars[2]

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

foreach (mycar mc in cars) { mc.newTest(); } 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 { 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:

So When 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

Related Documents

Polymorphism
June 2020 9
14 Polymorphism
November 2019 9
Inheritance And Polymorphism
November 2019 12
Virtual
June 2020 19
Virtual
June 2020 18