tag:blogger.com,1999:blog-6633841189462964052.post3389793564733938148..comments2024-01-03T00:37:48.870-08:00Comments on ASP.NET Tech Stuffs: Override non-virtual (nonoverridable in vb.net) methodsAbhishekhttp://www.blogger.com/profile/13666599807723810311noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-6633841189462964052.post-32005981069783809832011-04-15T05:58:17.351-07:002011-04-15T05:58:17.351-07:00You are right. Generally if the author of the clas...You are right. Generally if the author of the class thinks that someone can use my class but may give different behavior, he would prefer to declare those methods (behavior) as virtual and let any derived class give that its own desired implementation.<br />That means if something is not marked virtual (overridable in vb.net) it has the implicit assumption that behavior needs to be intact with the usage of the class.<br />Hiding base class function implementation as mentioned in the article is not a good practice but comes handy when you desperately want to change the behaviors. Ideally in these scenarios one should consult with the author and try to walk into his shoes and understand the reason for not making method overridable/virtual.Abhishekhttps://www.blogger.com/profile/13666599807723810311noreply@blogger.comtag:blogger.com,1999:blog-6633841189462964052.post-72869664903065819352011-04-15T05:12:23.599-07:002011-04-15T05:12:23.599-07:00Method hiding is for exactly those times when you ...Method hiding is for exactly those times when you need to have two things to have the same name but different behavior, otherwise it should not be used.<br />Real world example - <br />abstract class Food {<br /> public decimal TaxRate { get { return 7.0m;} }<br />}<br />abstract class Grocery : Food {<br /> new public decimal TaxRate { get { return 0.0m; } }<br />}<br />class Cake : Grocery {<br /> new public decimal TaxRate { get { return 7.0m; } }<br />}<br /><br />Above example is not designed very well but there can not be better name than "TaxRate" in this example.On the top of that assume abstract Food class is in 3rd party dll and you are required to use it but can not modify the method name.Shailendra Pratap Singhhttps://www.blogger.com/profile/08802904112903214340noreply@blogger.com