Inheritance with this base Class
Inheritance with this base Class
Inheritance With the Base Class
Inheriting the Base Object
After creating a derived class, to let you access the parent class, the C# language provides a keyword named base that gives a child class access to public and protected members of its parent class.
Practical Learning: Accessing the Base Object from a Child Class
namespace Geometry12 { public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } } }
namespace Geometry12 { public class TrapezoidalPrism : Trapezoid { private double len; public double Length { get { return len; } set { len = value; } } public double BaseArea { get { // "base" refers to a parent's property return base.Area; } } public double TopArea { get { // "base" TopBase refers to a parent's property return base.TopBase * Length; } } public double BottomArea { get { // "base" BottomBase refers to a parent's property return base.BottomBase * Length; } } public double Volume { get { // "base" Area refers to a parent's property return base.Area * Length; } } } }
Control | (Name) | Text |
PictureBox | ||
Label | Top Base: | |
TextBox | txtTopBase | |
Label | Bottom Base: | |
TextBox | txtBottomBase | |
Label | Height: | |
TextBox | txtHeight | |
Label | Length | |
TextBox | txtLength | |
Button | btnCalculate | Calculate |
Label | Base Area: | |
TextBox | txtBaseArea | |
Label | Top Area: | |
TextBox | txtTopArea | |
Label | Bottom Area: | |
TextBox | txtBottomArea | |
Label | Volume: | |
TextBox | txtVolume |
using System; using System.Windows.Forms; namespace Geometry12 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { double top = 0.00; double bottom = 0.00; double height = 0.00; double length = 0.00; TrapezoidalPrism tp = new TrapezoidalPrism(); try { top = double.Parse(txtTopBase.Text); } catch (FormatException exc) { MessageBox.Show(exc.Message, "Geometry"); } try { bottom = double.Parse(txtBottomBase.Text); } catch (FormatException exc) { MessageBox.Show(exc.Message, "Geometry"); } try { height = double.Parse(txtHeight.Text); } catch (FormatException exc) { MessageBox.Show(exc.Message, "Geometry"); } try { length = double.Parse(txtLength.Text); } catch (FormatException exc) { MessageBox.Show(exc.Message, "Geometry"); } tp.TopBase = top; tp.BottomBase = bottom; tp.Height = height; tp.Length = length; txtBaseArea.Text = tp.BaseArea.ToString(); txtTopArea.Text = tp.TopArea.ToString(); txtBottomArea.Text = tp.BottomArea.ToString(); txtVolume.Text = tp.Volume.ToString(); } } }
Inheriting the Base Constructors
If a parent class has a constructor, you can call that constructor in the child class. The constructor is accessed using the base keyword preceded by a colon. The keyword is accessed as when calling a method but after the parentheses of a constructor in the child class. That is, it must use parentheses. The primary formula to follow is:
class class-name : parent-name { access-level class-name(parameter(s)) : base(parameter(s)) }
To call the base() constructor in a constructor of a child class, there are a few rules you must follow:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
}
public class Employee : Person
{
public double HourlySalary { get; set; }
}
The error is because the Employee class must have at least one constructor (a constructor you must create). One way to avoid this error is to create a parameter-less constructor in the parent class. Here is an example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person()
{
}
public Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
}
public class Employee : Person
{
public double HourlySalary { get; set; }
}
To use the base() constructor, the constructor on it is applied in the child class and must use more than the number of parameters and same type(s) of parameter(s) as the constructor of the parent class.
To call a constructor of the parent class from the derived class, if you are calling a constructor that doesn't use any parameter, leave the parentheses empty. Here is an example:
public class Circle { private double _radius; public Circle() { _radius = 0.00; } } public class Cone : Circle { private double _height; public Cone() : base() { _height = 0.00; } }
If you are calling a constructor that uses one parameter, in the parentheses of base(), type the name of the parameter. Here is an example:
using System; public class Circle { private double _radius; public Circle(double rad) { _radius = rad; } } public class Cone : Circle { private double _height; public Cone(double rad, double hgt) : base(rad) { _height = hgt; } }
If the constructor of the parent class uses more than one parameter, in the parentheses of base, enter the names of the parameters of the child contructor that is calling it. Here is an example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
}
public class Employee : Person
{
public Employee(string fName, string lName) : base(fName, lName)
{
}
}
To make your code easy to read, you can call the base() constructor on the next line. Here is an example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string fName, string lName)
{
FirstName = fName;
LastName = lName;
}
}
public class Employee : Person
{
public Employee(string fName, string lName)
: base(fName, lName)
{
}
}
If the child construtor uses more parameters than the parent constructor, you can initialize the extra parameter(s) in the body of the (child) constructor. Here is an example:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public Person(string fName, string lName) { FirstName = fName; LastName = lName; } } public class Employee : Person { public Employee(string fName, string lName, double salary) : base(fName, lName) { HourlySalary = salary; } public double HourlySalary { get; set; } }
Practical Learning: Inheriting A Base Constructor
namespace Geometry12
{
public class Trapezoid
{
public double TopBase { get; set; }
public double BottomBase { get; set; }
public double Height { get; set; }
public Trapezoid(double top, double bottom, double height)
{
TopBase = top;
BottomBase = bottom;
Height = height;
}
public double Area
{
get
{
return Height * (TopBase + BottomBase) / 2.00;
}
}
}
}
namespace Geometry12
{
public class TrapezoidalPrism: Trapezoid
{
private double len;
public TrapezoidalPrism(double top, double bottom, double height, double length)
: base(top, bottom, height)
{
Length = length;
}
. . . No Change
}
}
using System;
using System.Windows.Forms;
namespace Geometry12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double top = 0.00;
double bottom = 0.00;
double height = 0.00;
double length = 0.00;
try
{
top = double.Parse(txtTopBase.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try { bottom = double.Parse(txtBottomBase.Text); }
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try
{
height = double.Parse(txtHeight.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try
{
length = double.Parse(txtLength.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
TrapezoidalPrism tp = new TrapezoidalPrism(top, bottom, height, length);
Present(tp);
}
void Present(TrapezoidalPrism shape)
{
txtBaseArea.Text = shape.BaseArea.ToString();
txtTopArea.Text = shape.TopArea.ToString();
txtBottomArea.Text = shape.BottomArea.ToString();
txtVolume.Text = shape.Volume.ToString();
}
}
}
Inheritance With this Class
Inheritance With this Object
We already know that every non-static class is equipped with an object named this. If you derive a class from a parent class, the child class has direct access to all public, internal, and protected members of the parent class. As an alternative to indicate that you are accessing a local member or a member of the parent class, in the child class, you can start the name of the member with .this.
Practical Learning: Inheriting this Parent
namespace Geometry12 { public class TrapezoidalPrism: Trapezoid { private double len; public TrapezoidalPrism(double top, double bottom, double height, double length) : base(top, bottom, height) { // "this" refers to a local property this.Length = length; } public double Length { get { // "this" refers to a local field return this.len; } set { // "this" refers to a local field this.len = value; } } public double BaseArea { get { // "base" refers to a parent return base.Area; } } public double TopArea { get { // "this" TopBase refers to a parent's property // "this" refers to a local property return this.TopBase * this.Length; } } public double BottomArea { get { // "this" BottomBase refers to a parent's property // "this" refers to a local property return this.BottomBase * this.Length; } } public double Volume { get { // "base" Area refers to a parent's property // "this" refers to a local property return base.Area * this.Length; } } } }
namespace Geometry12
{
public class Trapezoid
{
public double TopBase { get; set; }
public double BottomBase { get; set; }
public double Height { get; set; }
public Trapezoid(double top, double bottom, double height)
{
TopBase = top;
BottomBase = bottom;
Height = height;
}
public double Area => Height * (TopBase + BottomBase) / 2.00;
}
}
namespace Geometry12 { public class TrapezoidalPrism : Trapezoid { private double len; public TrapezoidalPrism(double top, double bottom, double height, double length) : base(top, bottom, height) { Length = length; } public double Length { get => len; set => len = value; } public double BaseArea { // "base" refers to a parent's property get => base.Area; } public double TopArea { // "base" TopBase refers to a parent's property get => base.TopBase * Length; } public double BottomArea { // "base" BottomBase refers to a parent's property get => base.BottomBase * Length; } public double Volume { // "base" Area refers to a parent's property get => base .Area * Length; } } }
using System;
using System.Windows.Forms;
namespace Geometry12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double top = 0.00;
double bottom = 0.00;
double height = 0.00;
double length = 0.00;
try
{
top = double.Parse(txtTopBase.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try { bottom = double.Parse(txtBottomBase.Text); }
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try
{
height = double.Parse(txtHeight.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
try
{
length = double.Parse(txtLength.Text);
}
catch (FormatException exc)
{
MessageBox.Show(exc.Message, "Geometry");
}
TrapezoidalPrism tp = new TrapezoidalPrism(top, bottom, height, length);
Present(tp);
}
void Present(TrapezoidalPrism shape)
{
txtBaseArea.Text = shape.BaseArea.ToString();
txtTopArea.Text = shape.TopArea.ToString();
txtBottomArea.Text = shape.BottomArea.ToString();
txtVolume.Text = shape.Volume.ToString();
}
}
}
namespace Geometry12
{
public class TrapezoidalPrism : Trapezoid
{
private double len;
// "this" refers to a local property
public TrapezoidalPrism(double top, double bottom, double height, double length)
: base(top, bottom, height) => this.Length = length;
public double Length
{
// "this" refers to a local field
get => this.len;
// "this" refers to a local field
set => this.len = value;
}
public double BaseArea
{
// "base" refers to a parent
get => base.Area;
}
public double TopArea
{
// "this" TopBase refers to a parent's property
// "this" refers to a local property
get => this.TopBase * this.Length;
}
public double BottomArea
{
// "this" BottomBase refers to a parent's property
// "this" refers to a local property
get => this.BottomBase * this.Length;
}
public double Volume
{
// "base" Area refers to a parent's property
// "this" refers to a local property
get => base.Area * this.Length;
}
}
}
using System; using System.Windows.Forms; namespace Geometry12 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } TrapezoidalPrism Prepare() { double top = 0.00; double bottom = 0.00; double height = 0.00; double length = 0.00; try { top = double.Parse(txtTopBase.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the top base", "Geometry"); } try { bottom = double.Parse(txtBottomBase.Text); } catch (FormatException) { MessageBox.Show("The value you specified for the bottom base is not valie", "Geometry"); } try { height = double.Parse(txtHeight.Text); } catch (FormatException) { MessageBox.Show("Either you didn't provide a value for the height or you left it empty.", "Geometry"); } try { length = double.Parse(txtLength.Text); } catch (FormatException) { MessageBox.Show("Please make sure you enter an appropriate value for the length.", "Geometry"); } return new TrapezoidalPrism(top, bottom, height, length); } private void btnCalculate_Click(object sender, EventArgs e) { TrapezoidalPrism tp = Prepare(); Present(tp); } void Present(TrapezoidalPrism shape) { txtBaseArea.Text = shape.BaseArea.ToString(); txtTopArea.Text = shape.TopArea.ToString(); txtBottomArea.Text = shape.BottomArea.ToString(); txtVolume.Text = shape.Volume.ToString(); } } }
using static System.Math; namespace Geometry13 { public class Cylinder { public double Length { get; set; } public double Radius { get; set; } public double Diameter => this.Radius * 2.00; public double Circumference => this.Diameter * PI; public double CrossArea => this.Radius * this.Radius * PI; public double LateralArea => this.Circumference * this.Length; public double CentralVolume => this.CrossArea * this.Length; } }
Control | (Name) | Text |
PictureBox | ||
Label | Radius: | |
TextBox | txtRadius | |
Label | Length: | |
TextBox | txtLength | |
Button | btnCalculate | Calculate |
Label | Diameter: | |
TextBox | txtDiameter | |
Label | Circumference: | |
TextBox | txtCircumference | |
Label | Cross Area: | |
TextBox | txtCrossArea | |
Label | Lateral Area: | |
TextBox | txtLateralArea | |
Label | Volume: | |
TextBox | txtVolume |
using System; using System.Windows.Forms; namespace Geometry13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void Present(Cylinder shape) { txtDiameter.Text = shape.Diameter.ToString(); txtCircumference.Text = shape.Circumference.ToString(); txtCrossArea.Text = shape.CrossArea.ToString(); txtLateralArea.Text = shape.LateralArea.ToString(); txtVolume.Text = shape.CentralVolume.ToString(); } private void btnCalculate_Click(object sender, EventArgs e) { double radius = 0.00; double length = 0.00; Cylinder body = new Cylinder(); try { radius = Convert.ToDouble(txtRadius.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the radius of the cylinder.", "Geometry - Cylinder"); } try { length = Convert.ToDouble(txtLength.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the radius of the cylinder.", "Geometry - Cylinder"); } body.Radius = radius; body.Length = length; Present(body); } } }
using static System.Math; namespace Geometry13 { public class Tank : Cylinder { public double Width { get { return this.Diameter; } } public double TotalLength { get { return this.Length + this.Radius + this.Radius; } } public double TotalArea { get { // Area on one side (half sphere) = Area of Sphere / 2 // Areas on both sides = area of a sphere = radius * radius * 4 * PI // Total External Area = lateral area (of the central cylinder) + areas on both sides return this.LateralArea + (this.Radius * this.Radius * PI * 4.00); } } public double TotalVolume { get { // Volume on one side (half sphere) = Volue of Sphere / 2 // Volumes on both sides = volume of a sphere = radius * radius * radius * PI * 4 / 3 // Total Volume = central volume + volumes on both sides (which is the volume of a sphere) return this.CentralVolume + (this.Radius * this.Radius * this.Radius * PI * 4.00 / 3.00); } } } }
Control | (Name) | Text | Other Properties |
Label | Radius: | ||
TextBox | txtRadius | ||
Label | ______________________ | ||
Label | Cylinder Length: | ||
TextBox | txtLength | ||
PictureBox | Image: Tank | ||
Button | btnCalculate | Calculate | |
Label | Each Side (Half Sphere): | Font: Georgia, 12pt, Italic, Bold, Underline | |
Label | Diameter: | ||
TextBox | txtDiameter | ||
Label | Circumference: | ||
TextBox | txtCircumference | ||
Label | Base Area: | ||
TextBox | txtCrossArea | ||
Label | The Central Cylinder | Font: Georgia, 12pt, Italic, Bold, Underline | |
Label | Lateral Area: | ||
TextBox | txtLateralArea | ||
Label | Central Volume: | ||
TextBox | txtCentralVolume | ||
Label | The Tank as a Whole | Font: Georgia, 12pt, Italic, Bold, Underline | |
Label | Through Width: | ||
TextBox | txtWidth | ||
Label | Total Length: | ||
TextBox | txtLateralLength | ||
Label | Total Area: | ||
TextBox | txtTotalArea | ||
Label | Total Volume: | ||
TextBox | txtTotalVolume |
using System; using System.Windows.Forms; namespace Geometry13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void Present(Tank vol) { txtDiameter.Text = vol.Diameter.ToString(); txtCircumference.Text = vol.Circumference.ToString(); txtCrossArea.Text = vol.CrossArea.ToString(); txtLateralArea.Text = vol.LateralArea.ToString(); txtCentralVolume.Text = vol.CentralVolume.ToString(); txtWidth.Text = vol.Width.ToString(); txtLateralLength.Text = vol.TotalLength.ToString(); txtTotalArea.Text = vol.TotalArea.ToString(); txtTotalVolume.Text = vol.TotalVolume.ToString(); } private void btnCalculate_Click(object sender, EventArgs e) { double radius = 0.00; double length = 0.00; Tank whole = new Tank(); try { radius = Convert.ToDouble(txtRadius.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the radius of the cylinder.", "Geometric Volume - Tank"); } try { length = Convert.ToDouble(txtLength.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the radius of the cylinder.", "Geometric Volume - Tank"); } whole.Radius = radius; whole.Length = length; Present(whole); } } }
Returning this Object
You may remember that a method of a class can return this object that represents the class itself. Here is an example:
public class Triangle
{
public void Examine()
{
Triangle inside = this;
}
}
Practical Learning: Returning this Object
using System;
namespace Geometry14
{
public class Circle
{
public double Radius { get; set; }
public Circle(double radius)
{
Radius = radius;
}
public double Diameter => Radius * 2.00;
public double Circumference => Diameter * Math.PI;
public double Area => Radius * Radius * Math.PI;
public Circle Encircle()
{
return this;
}
}
}
using System; namespace Geometry14 { public class Cone : Circle { public double Height { get; set; } public Cone(double radius, double height) : base(radius) { Height = height; } public double BaseArea => Area; public double Volume => Radius * Radius * Height * Math.PI / 3.00; } }
Getting this Parent
A method of an inherited class can return this. You must then identify the role of this object as it is used in a derived class.
Practical Learning: Getting this Parent
using System;
namespace Geometry14
{
public class Cone : Circle
{
public double Height { get; set; }
public Cone(double radius, double height) : base(radius)
{
Height = height;
}
public double BaseArea => Area;
public double Volume => Radius * Radius * Height * Math.PI / 3.00;
public Circle Create()
{
return this;
}
public Cone Surround()
{
return this;
}
}
}
Control | (Name) | Text | Font |
Label | Geometry - Cone | Times New Roman, 24pt, Bold | |
Label | ______________________ | ||
PictureBox | Image: Cone.png | ||
Label | Base Radius:: | ||
TextBox | txtBaseRadius | ||
Label | Height: | ||
TextBox | txtHeight | ||
Button | btnCalculate | Calculate | |
Label | Base | Georgia, 12pt, Bold, Italic, Underline | |
Label | Diameter: | ||
TextBox | txtDiameter | ||
Label | Circumference: | ||
TextBox | txtCircumference | ||
Label | Base Area: | ||
TextBox | txtBaseArea | ||
Label | Cone | Georgia, 12pt, Bold, Italic, Underline |
using System;
using System.Windows.Forms;
namespace Geometry14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double radius = 0.00;
double height = 0.00;
Cone dongle = new Cone(0.00, 0.00);
Circle c = dongle.Create();
try
{
radius = Convert.ToDouble(txtBaseRadius.Text);
}
catch (FormatException)
{
MessageBox.Show("You must provide a valid value for the radius of the base of the cone.", "Geometric Volume - Cone");
}
try
{
height = Convert.ToDouble(txtHeight.Text);
}
catch (FormatException)
{
MessageBox.Show("You must provide a valid value for the height of the cone.", "Geometric Volume - Cone");
}
Circle round = new Circle(radius);
dongle = new Cone(radius, height);
c = dongle.Create();
txtDiameter.Text = c.Diameter.ToString();
txtCircumference.Text=c.Circumference.ToString();
txtBaseArea.Text=c.Area.ToString();
txtVolume.Text = "0";
}
}
}
using System; using System.Windows.Forms; namespace Geometry14 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { double radius = 0.00; double height = 0.00; Cone dongle = new Cone(0.00, 0.00); Cone fancy = new Cone(0.00, 0.00); Circle c = dongle.Create(); try { radius = Convert.ToDouble(txtBaseRadius.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the radius of the base of the cone.", "Geometric Volume - Cone"); } try { height = Convert.ToDouble(txtHeight.Text); } catch (FormatException) { MessageBox.Show("You must provide a valid value for the height of the cone.", "Geometric Volume - Cone"); } dongle = new Cone(radius, height); fancy = dongle.Surround(); txtDiameter.Text = fancy.Diameter.ToString(); txtCircumference.Text = fancy.Circumference.ToString(); txtBaseArea.Text = fancy.Area.ToString(); txtVolume.Text = fancy.Volume.ToString(); } } }
Comparing an Object to this Parent
Remember that you can compare an object to this to find out what that object represents. This operation can be used to find out if an object refers to a parent, grand-parent, etc, of the object.
How "this" and "base" Objects are Similar and Different
The this and the base keywords live in a strange world. They have simmilarities and difference. Consider the following program:
using System; public class Program { public static void Main() { Rectangle rect = new Rectangle(); rect.Side = 248.97; rect.Height = 69.37; Console.Write("Length: "); Console.WriteLine(rect.Side); Console.Write("Height: "); Console.WriteLine(rect.Height); Console.Write("Area: "); Console.WriteLine(rect.RectArea); Console.WriteLine("========================"); return; } } public class Square { double len; public double Side { get { return len; } set { len = value; } } public double SquareArea { get { return Side * Side; } } } public class Rectangle : Square { double hgt; public double Height { get { return hgt; } set { hgt = value; } } public double RectArea { get { return Side * Height; } } }
This would produce:
Length: 248.97 Height: 69.37 Area: 17271.0489 ======================== Press any key to continue . . .
As one of the similarities, if you create a class that is based on another class, if the parent and the child classes don't have a member that uses the same name (in which case you would have a member in the child class and that member with that same name also exists in the parent class), both the this and the base keywords can be used interchangeably in the child class. In this case, both keywords would point to the same member, because that member is unique in both classes. Consider the following example:
public class Square { double len; public double Side { get { return len; } set { len = value; } } public double SquareArea { get { return Side * Side; } } } public class Rectangle : Square { double hgt; public double Height { get { return hgt; } set { hgt = value; } } public double RectArea { get { /* On the following lines, either the "this" or the "base" * keywords can be use and they have the exact same effect. */ return base.Side * Height; return this.Side * Height; } } }
In this case also, either of these keywords is useless.
One of the differentces between the this and the base keywords is that while the this keyword can be used in any non-static class the base keyword can be used only in derived classes. Another difference is that, in a derived class in which you pass a certain parameter to a constructor and that parameter must represent a member (field or property) of a parent class, only the base keyword, and not the this object, can help you identify the member of the parent class. Consider the following example:
public class Square
{
double len;
public Square(double side)
{
}
}
public class Rectangle : Square
{
public Rectangle(double width, double height)
: base(width) // This will not work: : this(width)
{
Height = height;
}
public double Height
{
get { return hgt; }
set { hgt = value; }
}
}
Probably the most important aspect that distinguishes the this and the base keywords is this. If you have a parent and a child classes and both have a member that uses the same name, if you want to access the parent member in the child class, in the child class, you must precede the name of the parent member with .base. Here is an example:
public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } // This (parent) class contains a property named Area public double Area => Height * (TopBase + BottomBase) / 2.00; } public class TrapezoidalPrism : Trapezoid { public double Length { get; set; } // This child class also contains a property named Area public new double Area { get { return BaseArea + TopArea + BottomArea + BaseArea; } } public double BaseArea { /* Here, the "base" keyword must be used to indicate that you are * accessing a member from the parent class while this class also * contains a member that has the same name as a member from the parent class. */ get => base.Area; } /* In the following properties, either the "this" or the "base" keyword can be used or * simply be omitted because there is no confusion about the member that is accessed. */ public double TopArea { get => this.TopBase * this.Length; } public double BottomArea { get => this.BottomBase * this.Length; } })
Introduction
Imagine you create a class, such as one for a geometric shape such as a trapezoid. As we saw above, you can use such a class as the base class for a prism. Both the trapezoid and its related prism have an area but their areas are different.
If you create or declare a new member in a derived class and that member has the same name as a member of the base class, when creating the new member, you may want to indicate to the compiler that you want to create a brand new and independent version of that method. When doing this, you would be asking the compiler to hide the member of the base class that has the same name, when the member of the current class is invoked.
Creating a New Version of a Member of a Class
To create a new version of a member, type the new keyword to its left.
Practical Learning: Creating a New Version of a Member
namespace Geometry12
{
public class TrapezoidalPrism : Trapezoid
{
private double len;
// "this" refers to a local property
public TrapezoidalPrism(double top, double bottom, double height, double length)
: base(top, bottom, height) => this.Length = length;
public double Length
{
// "this" refers to a local field
get => this.len;
// "this" refers to a local field
set => this.len = value;
}
public double BaseArea
{
// "base" refers to a parent
get => base.Area;
}
public double TopArea
{
// "this" TopBase refers to a parent's property
// "this" refers to a local property
get => this.TopBase * this.Length;
}
public double BottomArea
{
// "this" BottomBase refers to a parent's property
// "this" refers to a local property
get => this.BottomBase * this.Length;
}
public new double Area
{
get
{
return BaseArea + TopArea + BottomArea + BaseArea;
}
}
public double Volume
{
// "base" Area refers to a parent's property
// "this" refers to a local property
get => base.Area * this.Length;
}
}
}
Control | (Name) | Text |
Label | Tota Area: | |
TextBox | txtTotalArea |
using System;
using System.Windows.Forms;
namespace Geometry09
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TrapezoidalPrism Prepare()
{
double top = 0.00;
double bottom = 0.00;
double height = 0.00;
double length = 0.00;
try
{
top = double.Parse(txtTopBase.Text);
}
catch (FormatException)
{
MessageBox.Show("You must provide a valid value for the top base", "Geometry");
}
try { bottom = double.Parse(txtBottomBase.Text); }
catch (FormatException)
{
MessageBox.Show("The value you specified for the bottom base is not valie", "Geometry");
}
try
{
height = double.Parse(txtHeight.Text);
}
catch (FormatException)
{
MessageBox.Show("Either you didn't provide a value for the height or you left it empty.", "Geometry");
}
try
{
length = double.Parse(txtLength.Text);
}
catch (FormatException)
{
MessageBox.Show("Please make sure you enter an appropriate value for the length.", "Geometry");
}
return new TrapezoidalPrism(top, bottom, height, length);
}
private void btnCalculate_Click(object sender, EventArgs e)
{
TrapezoidalPrism tp = Prepare();
Present(tp);
}
void Present(TrapezoidalPrism shape)
{
txtBaseArea.Text = shape.BaseArea.ToString();
txtTopArea.Text = shape.TopArea.ToString();
txtBottomArea.Text = shape.BottomArea.ToString();
txtTotalArea.Text = shape.Area.ToString();
txtVolume.Text = shape.Volume.ToString();
}
}
}
|
||
Previous | Copyright © 2001-2021, FunctionX | Next |
|