Polymorphism
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 is said to be a virtual member of the class.
Practical Learning: Introcing Abstraction
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Geometry12.App_Code { 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;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Geometry12.App_Code
{
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;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Geometry12.App_Code
{
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;
}
}
}
<!DOCTYPE html> <html> <head> <style> .container { margin: auto; width: 550px; } .col-empty { width: 40px } .col-format { width: 120px } </style> <title>Geometry - Square</title> </head> <body> <h2 style="text-align: center">Geometry - Square</h2> <p style="text-align: center"><img src="~/Images/Square.png" alt="Geometry - Square" width="483" height="305" /></p> @{ double side = 0.00; Geometry12.App_Code.Square sqr = new Geometry12.App_Code.Square(0.00); if (IsPost) { side = Convert.ToDouble(Request["txtSide"]); sqr = new Geometry12.App_Code.Square(side); } } <div class="container"> <form name="frmGeometry" method="post"> <table> <tr> <td style="width: 80px">Side:</td> <td><input type="text" name="txtSide" style="width: 60px" value="@side" /></td> <td><input type="submit" name="btnCalculate" value="Calculate" /></td> <td>Perimter:</td> <td><input type="text" name="txtPerimter" value="@sqr.Perimeter" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td>Area:</td> <td><input type="text" name="txtArea" value="@sqr.Area" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td>Inscribed Radius:</td> <td><input type="text" name="txtInscribedRadius" value="@sqr.CalculateInradius()" /></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td>Circumscribed Radius:</td> <td><input type="text" name="txtCircumscribedRadius" value="@sqr.CalculateCircumradius()" /></td> </tr> </table> </form> </div> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Geometry13.App_Code { 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; } } }
<!DOCTYPE html> <html> <head> <title>Geometry - Trapezoid</title> <style> .container { margin: auto; width: 620px; } </style> </head> <body> @{ double shortSide = 0.00; double longSide = 0.00; double border = 0.00; Geometry13.App_Code.Trapezoid trap = new Geometry13.App_Code.Trapezoid(sSide : 0.00, lSide : 0.00, edge : 0.00); if (IsPost) { shortSide = Convert.ToDouble(Request["txtShortSide"]); longSide = Convert.ToDouble(Request["txtLongSide"]); border = Convert.ToDouble(Request["txtEdge"]); trap = new Geometry13.App_Code.Trapezoid(sSide : shortSide, lSide : longSide, edge: 0.00); } } <div class="container"> <h2 style="text-align: center">Geometry - Trapezoid</h2> <form name="frmGeometry" method="post"> <table> <tr> <td style="width: 375px" rowspan="9"> <img src="~/images/trapezoid2.png" width="373" height="273" alt="Geometry - Trapezoid"> </td> <td style="width: 150px">Short Side:</td> <td><input type="text" name="txtShortSide" value="@shortSide" /></td> </tr> <tr> <td>Long Side:</td> <td><input type="text" name="txtLongSide" value="@longSide" /></td> </tr> <tr> <td>Edge:</td> <td><input type="text" name="txtEdge" value="@border" /></td> </tr> <tr> <td> </td> <td style="text-align: center"><input type="submit" name="btnSubmit" value="Calculate" /></td> </tr> <tr> <td>Height:</td> <td><input type="text" name="txtHeight" value="et.Height" /></td> </tr> <tr> <td>Perimeter:</td> <td><input type="text" name="txtPerimeter" value="et.Inradius" /></td> </tr> <tr> <td>Area:</td> <td><input type="text" name="txtArea" value="et.Area" /></td> </tr> </table> </form> </div> </body> </html>
using static System.Math; using System.Collections.Generic; using System.Linq; using System.Web; namespace Geometry14.App_Code { 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; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Geometry14.App_Code { 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 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Geometry14.App_Code
{
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;
}
}
<!DOCTYPE html> <html> <head> <style> .container { margin: auto; width: 650px; } </style> <title>Geometry - Trapezoidal Prism</title> </head> <body> @{ double shortSide = 0.00; double longSide = 0.00; double edge = 0.00; double length = 0.00; Geometry14.App_Code.TrapezoidalPrism tp = new Geometry14.App_Code.TrapezoidalPrism(shortSide, longSide, edge, length); if (IsPost) { shortSide = Convert.ToDouble(Request["txtShortSide"]); longSide = Convert.ToDouble(Request["txtLongSide"]); edge = Convert.ToDouble(Request["txtEdge"]); length = Convert.ToDouble(Request["txtLength"]); tp = new Geometry14.App_Code.TrapezoidalPrism(shortSide, longSide, edge, length); } } <div class="container"> <h2 style="text-align: center">Geometry - Trapezoid</h2> <form name="frmGeometry" method="post"> <table> <tr> <td> <img src="~/images/tp.png" width="373" height="273" alt="Geometry - Trapezoid"></td> <td> <p>Base</p> <table> <tr> <td style="width: 20px"> </td> <td style="width: 120px">Short Side:</td> <td><input type="text" name="txtShortSide" value="@shortSide" /></td> </tr> <tr> <td> </td> <td>Long Side:</td> <td><input type="text" name="txtLongSide" value="@longSide" /></td> </tr> <tr> <td> </td> <td>Edge:</td> <td><input type="text" name="txtEdge" value="@edge" /></td> </tr> </table> <p>Prism</p> <table> <tr> <td style="width: 20px"> </td> <td style="width: 120px">Length:</td> <td><input type="text" name="txtLength" value="@length" /></td> </tr> </table> <p style="text-align: center"><input type="submit" name="btnSubmit" value="Calculate" /></p> <p>Base</p> <table> <tr> <td style="width: 20px"> </td> <td style="width: 120px">Height:</td> <td><input type="text" name="txtHeight" value="@tp.Height" /></td> </tr> <tr> <td> </td> <td>Perimeter:</td> <td><input type="text" name="txtPerimeter" value="@tp.Perimeter" /></td> </tr> <tr> <td> </td> <td>Base Area:</td> <td><input type="text" name="txtBaseArea" value="@tp.BaseArea" /></td> </tr> </table> <p>Prism</p> <table> <tr> <td style="width: 20px"> </td> <td style="width: 120px">Total Area:</td> <td><input type="text" name="txt>TotalArea" value="@tp.CalculateArea()" /></td> </tr> <tr> <td> </td> <td>Volume:</td> <td><input type="text" name="txtVolume" value="@tp.Volume" /></td> </tr> </table> </td> </tr> </table> </form> </div> </body> </html>
|
||
Previous | Copyright © 2002-2019, FunctionX | Next |
|