Using Inheritance

Shape top: 0
Shape left: 8
Shape perimeter: 12.5663706143592
Shape area: 12.5663706143592
Shape top: 10
Shape left: 0
Shape perimeter: 62.8318530717959
Shape area: 1256.63706143592



Notes:

Interfaces

  1. Interfaces are similar to abstract classes, in that they both provide a template that you can use to create a new class
  2. The main difference between abstract classes and instances is that interfaces do not provide any implementation of class members.
  3. All items defined in the interface must exist in any class that implements the interface.
  4. In this example, watch what happens when we use an Interface, implement it in an Abstract class, and then use that class as a base for 2 others.
  5. Forgive me for being lazy. I named the classes with the "New" keyword at the front. Seeing I don't want multiple class names in the same namespace, and I didn't want to make a new namespace just for them, I decided to just rename them.




Front end code

    9             <asp:Literal ID="litOutput" runat="server" />



Source Code.

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 namespace BicNet.Projects.mcp.chapter3

   12 {

   13     public partial class Interfaces : System.Web.UI.Page

   14     {

   15         protected void Page_Load(object sender, EventArgs e)

   16         {

   17             System.Text.StringBuilder sbOutput = new System.Text.StringBuilder();

   18             //Show Header

   19             sbOutput.Append("<h2>Using Inheritance</h2>");

   20             //Create a circle

   21             NewCircle circle = new NewCircle();

   22             circle.Radius = 2;

   23             circle.Center(10, 2);

   24             //Create a sphear

   25             NewSphear sphear = new NewSphear();

   26             sphear.Radius = 10;

   27             sphear.Center(10, 20, 25);

   28             //Show info about each shape ***this is the kicker.

   29             ShowShapeInfo(circle,ref sbOutput);

   30             ShowShapeInfo(sphear, ref sbOutput);

   31 

   32             //show the output

   33             litOutput.Text = sbOutput.ToString();

   34         }

   35 

   36         private void ShowShapeInfo(IFigure shape,ref System.Text.StringBuilder sbOutput)

   37         {

   38             //Since shape argument is IFigure, we know that is has these members.

   39             sbOutput.Append(String.Format("Shape top: {0} <br/>", shape.Top));

   40             sbOutput.Append(String.Format("Shape left: {0} <br/>", shape.Left));

   41             sbOutput.Append(String.Format("Shape perimeter: {0} <br/>", shape.Perimeter()));

   42             sbOutput.Append(String.Format("Shape area: {0} <br/>", shape.Area()));

   43 

   44         }

   45     }

   46 

   47     public interface IFigure

   48     {

   49         double Top

   50         {

   51             get;

   52             set;

   53         }

   54         double Left

   55         {

   56             get;

   57             set;

   58         }

   59         double Area();

   60         double Perimeter();

   61     }

   62 

   63     public abstract class NewShape : IFigure

   64     {

   65         //Constructor

   66         public NewShape()

   67         { }

   68 

   69         public abstract double Top

   70         {

   71             get;

   72             set;

   73         }

   74         public abstract double Left

   75         {

   76             get;

   77             set;

   78         }

   79 

   80         public abstract double Area();

   81         public abstract double Perimeter();

   82     }

   83 

   84     public class NewSphear : NewCircle

   85     {

   86         double fzCenter = 0;

   87 

   88         public NewSphear()

   89         {

   90             fzCenter = 0;

   91         }

   92 

   93         public override double Area()

   94         {

   95             return (double)(4 * System.Math.PI * Math.Pow((double)base.Radius, 2));

   96         }

   97 

   98         public new void Center(double x, double y)

   99         {

  100             this.Center(x, y, 0);

  101         }

  102 

  103         public void Center(double x, double y, double z)

  104         {

  105             base.Center(x, y);

  106             fzCenter = z;

  107         }

  108 

  109         public double Volume()

  110         {

  111             return (double)((4 / 3) * System.Math.PI * Math.Pow((double)base.Radius, 3));

  112         }

  113 

  114         public double Front

  115         {

  116             get { return fzCenter - base.Radius; }

  117             set { fzCenter = value + base.Radius; }

  118         }

  119     }

  120 

  121     public class NewCircle : NewShape

  122     {

  123         double fxCenter = 0, fyCenter = 0, fRadius = 0;

  124 

  125         public NewCircle()

  126         {

  127             fxCenter = 0;

  128             fyCenter = 0;

  129             fRadius = 0;

  130         }

  131 

  132 

  133         public override double Top

  134         {

  135             get { return fyCenter - fRadius; }

  136             set { fyCenter = value + fRadius; }

  137         }

  138 

  139         public override double Left

  140         {

  141             get { return fxCenter - fRadius; }

  142             set { fxCenter = value + fRadius; }

  143         }

  144 

  145         public double Radius

  146         {

  147             get { return fRadius; }

  148             set { fRadius = value; }

  149         }

  150 

  151         public virtual void Center(double x, double y)

  152         {

  153             fxCenter = x;

  154             fyCenter = y;

  155         }

  156 

  157         public override double Area()

  158         {

  159             return (double)(System.Math.PI * Math.Pow((double)fRadius, 2));

  160         }

  161 

  162         public override double Perimeter()

  163         {

  164             return 2 * fRadius * (double)System.Math.PI;

  165         }

  166     }

  167 

  168 

  169 

  170 

  171 }