Let’s understand how we normally work first?
- We create class library with some functionality and reuse it at multiple places.
Note: Above code will be used by more than one client code.
2. Test our code
3. Deploy the application
Now sometime in the future we came across a situation where one of the client wants some changes in the MyMethod function and we start putting if condition inside the MyMethod function.
- Now because I have made changes in MyMethod function I have to test all my existing clients along with one who was seeking a change in class library.
- It also violates OCP – Open closed principle – Software modules should be closed for modifications but open for extensions.
Answer is polymorphism.
At the time of creation of the class we should have anticipated such issue and we should have decorated our MyFunction method with virtual modifier.
Now whenever changes will be asked we can go and create new child class and MyClass and override and MyMethod function. Client Code which was seeking a change in the functionality will use new class.
MyClass m=new MyClass();
Client seeking change
MyClass m=new NewMyClass();
Recently one of our friends in a .NET interview was asked about the importance of yield keyword , below goes the explanation for the same.
Hope you understood the concept and enjoyed reading it. Don’t forget to subscribe us on facebook.
If you are looking for some technical training on any Microsoft technologies likeWCF,MVC,WCF,BI or any practices like Design Patterns, UML, DDD etc visit www.sukesh-Marla.com or contact SukeshMarla@Gmail.com