Polymorphism |
|
Introduction
When you are creating a class, if you anticipate that a certain member should be redefined in a derived class, you can indicate this in your code. On the other hand, while creating a class that is based on another, if you find out that you are customizing a member that already exists in the base class, you should indicate, in your code, that you are providing a new version. In both cases, the common member should be created as virtual.
Practical Learning: Introcing Abstraction
using System; namespace Geometry15 { public class Square { public double Side { get; set; } public Square(double side) { Side = side; } public double Perimeter { get { return Side * 4.00; } } } }
A method is said to be virtual if it is anticipated to have different versions or implementations in classes that derive from it. To create a virtual method, in the base class, type the virtual keyword to the left of the return type of the method. If the method has an access level (private, public, or internal), the virtual keyword can appear before or after the access level.
Practical Learning: Creating Virtual Methods
using System;
namespace Geometry15
{
public class Square
{
public double Side { get; set; }
public Square(double side)
{
Side = side;
}
public double Perimeter
{
get
{
return Side * 4.00;
}
}
public virtual double CalculateInradius()
{
return Side / 2.00;
}
public virtual double CalculateCircumradius()
{
return Math.Sqrt(2.00) * Side / 2.00;
}
}
}
Virtual Properties
A property of a class is virtual if it may be implemented in a derived class. This means that the property should primarily be created in its original class.
To get a virtual property, when creating the class, add the virtual keyword before the return type of the property.
Practical Learning: Creating a Virtual Property
using System;
namespace Geometry15
{
public class Square
{
public double Side { get; set; }
public Square(double side)
{
Side = side;
}
public double Perimeter
{
get
{
return Side * 4.00;
}
}
public virtual double Area
{
get
{
return Side * Side;
}
}
public virtual double CalculateInradius()
{
return Side / 2.00;
}
public virtual double CalculateCircumradius()
{
return Math.Sqrt(2.00) * Side / 2.00;
}
}
}
Control | (Name) | Text | Other Properties |
PictureBox | Image: Square | ||
Label | Geometry - Square | Font: Georgia, 20, Bold | |
Label | ____________ | ||
Label | Side | ||
TextBox | txtSide | ||
Button | btnCalculate | Calculate | |
Label | Perimeter: | ||
TextBox | txtPerimeter | ||
Label | Area: | ||
TextBox | txtArea | ||
Label | Inscribed Radius: | ||
TextBox | txtInscribedRadius | ||
Label | Circumscribed Radius: | ||
TextBox | txtCircumscribedRadius |
using System;
using System.Windows.Forms;
namespace Geometry15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double side = 0.00;
try
{
side = Convert.ToDouble(txtSide.Text);
}
catch (FormatException)
{
MessageBox.Show("You must provide a valid value for the side of the square.", "Geometry - Square");
}
Square sqr = new Square(side);
txtPerimeter.Text = sqr.Perimeter.ToString();
txtArea.Text =sqr.Area.ToString();
txtInscribedRadius.Text =sqr.CalculateInradius().ToString();
txtCircumscribedRadius.Text = sqr.CalculateCircumradius().ToString();
}
}
}
using System; namespace Geometry16 { public class Trapezoid { public double ShortSide { get; set; } public double LongSide { get; set; } public double Edge { get; set; } public Trapezoid(double sSide, double lSide, double edge) { ShortSide = sSide; LongSide = lSide; Edge = edge; } public double Perimeter => LongSide + Edge + ShortSide + Edge; // http://mathworld.wolfram.com/IsoscelesTrapezoid.html public double Height { get { return Math.Sqrt((Edge * Edge) - (Math.Pow(LongSide - ShortSide, 2.00) / 4.00)); } } virtual public double CalculateArea() { return (LongSide + ShortSide) * Height / 2.00; } } }
Control | (Name) | Text | Other Properties |
PictureBox | Image: Trapezoid | ||
Label | Geometry - Square | Font: Georgia, 20, Bold | |
Label | _______________ | ||
Label | Short Side: | ||
TextBox | txtShortSide | ||
Label | Long Side: | ||
TextBox | txtLongSide | ||
Label | Edge: | ||
TextBox | txtEdge | ||
Button | btnCalculate | Calculate | |
Label | Height: | ||
TextBox | txtHeight | ||
Label | Perimeter: | ||
TextBox | txtPerimeter | ||
Label | Area: | ||
TextBox | txtArea |
using System; using System.Windows.Forms; namespace Geometry16 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { Trapezoid shape = CreateShape(); Show(shape); } void Show(object obj) { if (obj is null) return; if(obj is Trapezoid) { Trapezoid brick = (Trapezoid)obj; txtHeight.Text = brick.Height.ToString(); txtPerimeter.Text = brick.Perimeter.ToString(); txtArea.Text = brick.CalculateArea().ToString(); } } Trapezoid CreateShape() { double border = 0.00; double longSide = 0.00; double shortSide = 0.00; Trapezoid trap = new Trapezoid(sSide: 0.00, lSide: 0.00, edge: 0.00); try { shortSide = Convert.ToDouble(txtShortSide.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the short side of the trapezoid.", "Geometry - Trapezoid"); } try { longSide = Convert.ToDouble(txtLongSide.Text); } catch (FormatException) { MessageBox.Show("Make sure must provide a valid value for the short side of the trapezoid.", "Geometry - Trapezoid"); } try { border = Convert.ToDouble(txtEdge.Text); } catch (FormatException) { MessageBox.Show("If you forgot, or did sure must provide a valid value for the short side of the trapezoid.", "Geometry - Trapezoid"); } trap = new Trapezoid(sSide: shortSide, lSide: longSide, border); return trap; } } }
using static System.Math; namespace Geometry17 { public class Trapezoid { public double ShortSide { get; set; } public double LongSide { get; set; } public double Edge { get; set; } public Trapezoid(double sSide, double lSide, double side) { ShortSide = sSide; LongSide = lSide; Edge = side; } public double Perimeter => LongSide + Edge + ShortSide + Edge; // http://mathworld.wolfram.com/IsoscelesTrapezoid.html public double Height { get { return Sqrt((Edge * Edge) - (Pow(LongSide - ShortSide, 2.00) / 4.00)); } } virtual public double CalculateArea() { return (LongSide + ShortSide) * Height / 2.00; } } }
namespace Geometry17 { public class TrapezoidalPrism : Trapezoid { public double Length { get; set; } public TrapezoidalPrism(double sSide, double lSide, double side, double len) : base(sSide, lSide, side) { Length = len; } public double BaseArea => base.CalculateArea(); public double Volume => BaseArea * Length; } }
Overriding a Member in a Derived Class
As seen in previous sections, you can create a virtual method or property in a class and use it like any other method or property of its class. As mentioned in our introduction, the essence of creating a virtual method or property is to provide a different implementation of that member in its own and in classes that derive from that class. Providing a different version of a member or property in a derived class is referred to as overriding the method or property.
To override a method, in a derived class, create a method with the same syntax (same return type, same name, and same parameter(s) if any) as the method in the parent class. This time, replace the virtual keyword with the override keyword. In the same way, to override a propertty, create one of the same return type and name in the derived class but use the override keyword instead of the the virtual keyword.
Practical Learning: Overriding a Method in a Derived Class
namespace Geometry17
{
public class TrapezoidalPrism : Trapezoid
{
public double Length { get; set; }
public TrapezoidalPrism(double sSide, double lSide, double side, double len)
: base(sSide, lSide, side)
{
Length = len;
}
public double BaseArea => base.CalculateArea();
override public double CalculateArea()
{
double shortArea = ShortSide * Length;
double longArea = LongSide * Length;
double sideArea = Edge * Length;
return (BaseArea * 2.00) + shortArea + longArea + (sideArea * 2.00);
}
public double Volume => BaseArea * Length;
}
}
Control | (Name) | Text | Font |
Label | Geometry - Trapezoid | Font: Times New Roman, 24, Bold | |
Label | _______________ | Georgia, 20.25pt, style=Bold, Italic | |
PictureBox | Image: Trapezoid | ||
Label | Base ______________ | ||
Label | Short Side: | ||
TextBox | txtShortSide | ||
Label | Long Side: | ||
TextBox | txtLongSide | ||
Label | Edge: | ||
TextBox | txtEdge | ||
Label | Prism ______________ | ||
Label | Length: | ||
TextBox | txtLength | ||
Button | btnCalculate | Calculate | |
Label | Base ______________ | ||
Label | Height: | ||
TextBox | txtHeight | ||
Label | Perimeter: | ||
TextBox | txtPerimeter | ||
Label | Base Area: | ||
TextBox | txtBaseArea | ||
Label | Prism ______________ | ||
Label | Total Area: | ||
TextBox | txtTotalArea | ||
Label | Volume: | ||
TextBox | txtVolume |
using System; using System.Windows.Forms; namespace Geometry17 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } TrapezoidalPrism GetValue() { double edge = 0.00; double length = 0.00; double longSide = 0.00; double shortSide = 0.00; TrapezoidalPrism tp = new TrapezoidalPrism(shortSide, longSide, edge, length); try { shortSide = Convert.ToDouble(txtShortSide.Text); } catch (FormatException) { MessageBox.Show("Make sure you provide a valid value for the short side.", "Geometry - Trapezoidal Prism"); } try { longSide = Convert.ToDouble(txtLongSide.Text); } catch (FormatException) { MessageBox.Show("You must enter a value for the long side.", "Geometry - Trapezoidal Prism"); } try { edge = Convert.ToDouble(txtEdge.Text); } catch (FormatException) { MessageBox.Show("You are supposed to type an appropriate value for the edge.", "Geometry - Trapezoidal Prism"); } try { length = Convert.ToDouble(txtLength.Text); } catch (FormatException) { MessageBox.Show("You are supposed to type an appropriate value for the edge.", "Geometry - Trapezoidal Prism"); } return new TrapezoidalPrism(shortSide, longSide, edge, length); } void Display(TrapezoidalPrism prism) { if (prism is null) return; txtHeight.Text = prism.Height.ToString(); txtPerimeter.Text= prism.Perimeter.ToString(); txtBaseArea.Text = prism.BaseArea.ToString(); txtTotalArea.Text = prism.CalculateArea().ToString(); txtVolume.Text = prism.Volume.ToString(); } private void btnCalculate_Click(object sender, EventArgs e) { TrapezoidalPrism tp = GetValue(); Display(tp); } } }
|
||
Previous | Copyright © 2002-2021, FunctionX | Next |
|