Polymorphism-which is faster and betterhttp://forums.asp.net/t/1798467.aspx/1?Polymorphism+which+is+faster+and+betterMon, 30 Apr 2012 13:25:41 -040017984674958188http://forums.asp.net/p/1798467/4958188.aspx/1?Polymorphism+which+is+faster+and+betterPolymorphism-which is faster and better <pre class="prettyprint">public abstract class Shape { private string _Me = string.Empty; public string Me { get { return _Me; } } public void CallMe() { Console.WriteLine(&quot;i am calling from Base&quot;); } public void ColorShapes(Shape Shape) { if (Shape is Circle) { // calling circle dal } else if (Shape is Triangle) { } } } public class Circle : Shape { public string CircleID { get; set; } public string CircleLogo { get; set; } public string CircleColor { get; set; } public void CircleMe() { } } public class Triangle : Shape { public string TriangleID { get; set; } public string TriangleLogo { get; set; } public string TriColor { get; set; } public void TriMe() { } }</pre> <p></p> <p></p> <pre class="prettyprint"> //Shape _s = new Circle(); //((Circle)_s).CircleColor = "Red"; //((Circle)_s).CircleID = "1"; //((Circle)_s).CircleLogo = "Flag"; //_s.ColorShapes(_s); //((Circle)_s).CircleMe(); or this one Circle _c = new Circle(); _c.ColorShapes(_c);</pre> <p><br> <br> </p> 2012-04-30T12:26:48-04:004958287http://forums.asp.net/p/1798467/4958287.aspx/1?Re+Polymorphism+which+is+faster+and+betterRe: Polymorphism-which is faster and better <p>First thing your design is violating the polymorphism thing because in the abstract class you have a method ColorShapes that is typechecking for circle or triangle whereas these part of codes should be in their respective classes ie circle and triangle. Because abstract classes are meant to be inherited so they should not contain any concete object specific behaviour. Let me slightly modify your code as follows -</p> <pre class="prettyprint">public abstract class Shape { private string _Me = string.Empty; public string Me { get { return _Me; } } public void CallMe() { Console.WriteLine(&quot;i am calling from Base&quot;); } public abstract void ColorShapes(); //{ // //if (Shape is Circle) // //{ // // // calling circle dal // //} // //else if (Shape is Triangle) // //{ // //} //} } public class Circle : Shape { public string CircleID { get; set; } public string CircleLogo { get; set; } public string CircleColor { get; set; } public void CircleMe() { } public override void ColorShapes() { this.CircleMe(); } } public class Triangle : Shape { public string TriangleID { get; set; } public string TriangleLogo { get; set; } public string TriColor { get; set; } public void TriMe() { } public override void ColorShapes() { TriMe(); } }</pre> <p>Second thing is achieving polymorphism is always by using a common interface. So here is a proper usage of your classes. So every time colorshapes() method is called it is detemined at run time which of the two circle's or triangle's method be called.</p> <pre class="prettyprint"> Shape circle = new Circle(); circle.ColorShapes(); Shape triangle = new Triangle(); triangle.ColorShapes();</pre> <p>Hope you understood the concept.</p> <p><br> <br> </p> 2012-04-30T13:12:12-04:004958292http://forums.asp.net/p/1798467/4958292.aspx/1?Re+Polymorphism+which+is+faster+and+betterRe: Polymorphism-which is faster and better <p>ok Pankaj i got your point but what if the Shape Class is not an abstract class. just a Class which i am inheriting</p> <pre class="prettyprint">public Class Shape{} public Class Cricle:Shape {} public class Triangle:Shape{}</pre> <p>what will happen then. in that case...what will be solution </p> <p></p> 2012-04-30T13:18:07-04:004958305http://forums.asp.net/p/1798467/4958305.aspx/1?Re+Polymorphism+which+is+faster+and+betterRe: Polymorphism-which is faster and better <p>If shape class is not an abstract class so it will not have any abstract member. Basically this will be like a instantiable class. But if you think on a conceptual plane, shape class itself does not have an identity.I mean if i say draw a shape you will instantly ask what kind of shape should i draw,is it a circle, rectangle, triange or polygon.</p> <p>So shape class happen to be meaning ful when it is inherited by a concrete class like circle. So when i say draw a circle you instantly draw a circle.</p> <p>So abstract classes are used to manage the complexity of real world objects.</p> <p>If will be all the same if your class is not an abstract class. But to let the polymorphism work the ColorShapes() method must be virtual so that it is overridden by child classes like Circle or Triangle. The previous code will keep running.</p> <pre class="prettyprint">//Override to define the exact behaviour public virtual void ColorShapes() { }</pre> <p>Hope i could make you understand.</p> <p></p> 2012-04-30T13:25:41-04:00