User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper Importance and Use of Dynamic Polymorphism in C#

In today's world we implement lots of concept in our programming with the help of OOP. When we talk about OOP, Polymorphism & Inheritance are always on top priority. Polymorphism & inheritance are the main pillar of OOP concept. Today we will discuss about concepts of virtual, override, sealed, abstract and new keyword in C#. Here we have an important question- "With the help of these keywords, what we do in C#?"

Basically above keywords are used to implement the concept of Polymorphism in our C#. In order to understand the importance of these keywords and why we need that in our programming language, we will start discuss about - What is Polymorphism in OOP?

Polymorphism is a Greek word which means "many-shaped". In programming language we can say "one interface with multiple function".

There are two types of polymorphism used in C#:

 

1. Static -- Compile time

                * Method overloading

2. Dynamic -- Run time

 

After above points now move ahead with these keyword which are part of Dynamic Polymorphism. At this point we can say with the help of these keyword, we used Dynamic Polymorphism in C#.

We have two way through which we can implemented Dynamic Polymorphism in C#:

1. Method overriding

2. Method hiding

 

In the above discussion we understood the basic concepts of Polymorphism and their types. Now move ahead and start discussion about the above important keywords in C#. We start with Virtual keyword, here we have question - "What is meant by Virtual Keyword in C# and why would we need to use virtual methods?"

Virtual keyword is used in base class to inform child class to override that method. Child class override only that method which is defined virtual in base class. With virtual methods, the runtime type is always used to determine the best method implementation.

The important point that program is design in the way where we don't know all the type objects that will be used when code is executed. Mostly we designed the programming on base and child class relationship. So we can the behaviour of methods as per requirements. When you call a method on the base type, you invoke the more derived and required method.

After the virtual now move ahead and start about override. Override keyword use for re-defining the functionality of base class virtual function in child class.

Now we can say Virtual and Override keywords used to implement Method Overriding in C#.

Look at the following code:


  1. public class Vehicle
  2. {
  3. // Virtual method
  4. public virtual void Drive()
  5. {
  6. Console.WriteLine("Performing base class driving tasks");
  7. }
  8. }
  9. // Child class Inherit from base class
  10. public class Car : Vehicle
  11. {
  12. public override void Drive()
  13. {
  14. // Code to drive a car...
  15. Console.WriteLine("Driving a car");
  16. }
  17. }
  18. // Child class Inherit from base class
  19. public class Bike : Vehicle
  20. {
  21. public override void Drive()
  22. {
  23. // Code to drive a bike...
  24. Console.WriteLine("Driving a bike");
  25. }
  26. }
  27. class Program
  28. {
  29. static void Main(string[] args)
  30. {
  31. // Compile-time type is Vehicle.
  32. // Runtime type is Vehicle as well.
  33. Vehicle veh = new Vehicle();
  34. veh.Drive ();
  35. // Compile-time type is Vehicle.
  36. // Runtime type is Car.
  37. Vehicle car = new Car();
  38. car.Drive ();
  39. // Compile-time type is Vehicle.
  40. // Runtime type is Bike.
  41. Vehicle bike = new Bike();
  42. bike.Drive();
  43. // Keep the console open in debug mode.
  44. Console.WriteLine("Press any key to exit.");
  45. Console.ReadKey();
  46. }
  47. }


In the above code we can see the use of virtual and override keywords. We can see in Main method there is a Vehicle type object named "veh", veh has a compile-time and runtime type of Vehicle. In next line, there is a another Vehicle type object named "car", car has a compile-time type of Vehicle but a runtime type of Car. Now move ahead in next line where one more Vehicle type object named "bike", bike has a compile-time type of Vehicle but a runtime type of Bike. When we execute the above code, we get the following output:

/* Output:

    Performing base class driving tasks

    Driving a car

    Driving a bike

 */

 

 

In the above discussion I hope we understood the concepts of method overriding now move one step ahead, here we have an important question - "What is the concept of method hiding?"

There is a one more keyword in C# - new, with the help of this keyword we hide the reference of base class function in child class. Method hiding is also known as "Shadowing in C#".

For more details about method hiding please visit this article where we discussed specific about this concepts.

See more at : http://firstcrazydeveloper.com/Blogs/BlogView.html/76/what-is-the-use-of-shadowing-in-c

 After all the above discussion, we get that through the help of virtual and override we can change the behaviour of base class method into child class. But here we have an important question -- "How can we prevent to make any behaviour change of base class method?"

So we have here one more keyword in C# - "sealed", with the help of sealed keyword we can prevent method to overriding in child class. We can say sealed method used to prevent behaviour change in child class.

 

Look at the following code:

  1. public class Vehicle
  2. {
  3. // Virtual method
  4. public virtual void Drive()
  5. {
  6. Console.WriteLine("Performing base class driving tasks");
  7. }
  8. public virtual int Price(int price, int totalCount)
  9. {
  10. return price * totalCount;
  11. }
  12. }
  13. // Child class Inherit from base class
  14. public class Car : Vehicle
  15. {
  16. public override void Drive()
  17. {
  18. // Code to drive a car...
  19. Console.WriteLine("Driving a car");
  20. }
  21. public override sealed int Price(int price, int totalCount)
  22. {
  23. return price * totalCount;
  24. }
  25. }
  26. // Child class Inherit from base class
  27. public class Bike : Vehicle
  28. {
  29. public override void Drive()
  30. {
  31. // Code to drive a bike...
  32. Console.WriteLine("Driving a bike");
  33. }
  34. public override int Price(int price, int totalCount)
  35. {
  36. return price * totalCount;
  37. }
  38. }
  39. // Child class Inherit from base class
  40. public class BMW:Car
  41. {
  42. public override void Drive()
  43. {
  44. // Code to drive a car...
  45. Console.WriteLine("Driving a BMW car");
  46. }
  47. public override int Price(int price, int totalCount)
  48. {
  49. return price * totalCount;
  50. }
  51. }


In the above code we can see there is base class "Vehicle" and two child class "Car & Bike", which inherited from "Vehicle class". In "Vehicle" class there is a virtual method named "Price". In child class "Car" we changed the keyword and use "sealed" with Price method. There is a more class named "BMW", which inherited from Car class. Now again we tried to override the Price method. When we executed this code we get following error:

In the above image we can see when we tried to executed the above code we get error that we can't override the sealed method. Now we can say sealed method used to prevent override in child class.

In the above discussion we understood the concept, how can we prevent override in C#? Now it's magic that again I have an important question -- "How can we force to override method in child class?"

Ha ha ha ...!!!  

In the above question we found one more concept in C#. We can do that in C# with the help of abstract method. Here we have one more keyword - abstract. Abstract method in base class force to child class to implement their own logic.

Actually abstract method is a template method in base class without any body and we can write their logic in child class.

Look at the following code:

  1. public abstract class myVehicle
  2. {
  3. // Virtual method
  4. public virtual void Drive()
  5. {
  6. Console.WriteLine("Performing base class driving tasks");
  7. }
  8. public virtual int Price(int price, int totalCount)
  9. {
  10. return price * totalCount;
  11. }
  12. public abstract int CalculateVehiclePrice ();
  13. }
  14. // Child class Inherit from base class
  15. public class Car : myVehicle
  16. {
  17. public override void Drive()
  18. {
  19. // Code to drive a car...
  20. Console.WriteLine("Driving a car");
  21. }
  22. public override sealed int Price(int price, int totalCount)
  23. {
  24. return price * totalCount;
  25. }
  26. }

In the above code we can see there is base abstract class "myVehicle" and a child class "Car", which inherited from "myVehicle class". In "myVehicle" class there is an abstract method named "CalculateVehiclePrice". In child class we don’t implement the logic of abstract method of parent class. When we executed this code we get following error:

In the above image we can see when we tried to execute the above code we get error that we does not implement inherited abstract member. Now we can say abstract method used to force override in child class.

 

After all the above discussion I hope we understood the way of the use of virtual and override, new, sealed and abstract method in C#.

 

We have following important points here:

1. The Virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.

2. The Override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into derived class.

3. The New keyword is used to hide a method, property, indexer, or event of base class into derived class.

4. The Sealed keyword is used to prevent to override a method, property, indexer, or event of base class into derived class.

5. The Abstract keyword is used to force to override a method, property, indexer, or event of base class into derived class.

 

 

Happy reading!!

Abhishek Kumar


crazydeveloper Home Page 17 June 2015

Become a Fan