public abstract class Shape
{
private string _Me = string.Empty;
public string Me
{
get { return _Me; }
}
public void CallMe()
{
Console.WriteLine("i am calling from Base");
}
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() { }
}
//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);
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 -
public abstract class Shape
{
private string _Me = string.Empty;
public string Me
{
get { return _Me; }
}
public void CallMe()
{
Console.WriteLine("i am calling from Base");
}
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();
}
}
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.
Shape circle = new Circle();
circle.ColorShapes();
Shape triangle = new Triangle();
triangle.ColorShapes();
Hope you understood the concept.
Please “Mark as Answer” on the reply that helps you. This will help others jump directly to a solution having similar problem.
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.
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.
So abstract classes are used to manage the complexity of real world objects.
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.
//Override to define the exact behaviour
public virtual void ColorShapes()
{
}
Hope i could make you understand.
Please “Mark as Answer” on the reply that helps you. This will help others jump directly to a solution having similar problem.
tan_vision_1...
Participant
1178 Points
413 Posts
Polymorphism-which is faster and better
Apr 30, 2012 12:26 PM|LINK
public abstract class Shape { private string _Me = string.Empty; public string Me { get { return _Me; } } public void CallMe() { Console.WriteLine("i am calling from Base"); } 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() { } }//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);Pankaj.Sharm...
Contributor
2350 Points
387 Posts
Re: Polymorphism-which is faster and better
Apr 30, 2012 01:12 PM|LINK
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 -
public abstract class Shape { private string _Me = string.Empty; public string Me { get { return _Me; } } public void CallMe() { Console.WriteLine("i am calling from Base"); } 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(); } }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.
Shape circle = new Circle(); circle.ColorShapes(); Shape triangle = new Triangle(); triangle.ColorShapes();Hope you understood the concept.
tan_vision_1...
Participant
1178 Points
413 Posts
Re: Polymorphism-which is faster and better
Apr 30, 2012 01:18 PM|LINK
ok Pankaj i got your point but what if the Shape Class is not an abstract class. just a Class which i am inheriting
public Class Shape{} public Class Cricle:Shape {} public class Triangle:Shape{}what will happen then. in that case...what will be solution
Pankaj.Sharm...
Contributor
2350 Points
387 Posts
Re: Polymorphism-which is faster and better
Apr 30, 2012 01:25 PM|LINK
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.
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.
So abstract classes are used to manage the complexity of real world objects.
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.
//Override to define the exact behaviour public virtual void ColorShapes() { }Hope i could make you understand.