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 Geometry05 { 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 Geometry05 { 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; } } } }
using static System.Console; namespace Geometry05 { public class Geometry { private static void Main() { TrapezoidalPrism tp = new TrapezoidalPrism(); WriteLine("Geometry - Trapezoidal Prism"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Top Width: "); double top = double.Parse(ReadLine()); Write("Bottom Width: "); double bottom = double.Parse(ReadLine()); Write("Length: "); double length = double.Parse(ReadLine()); Write("Height: "); double height = double.Parse(ReadLine()); tp.TopBase = top; tp.BottomBase = bottom; tp.Height = height; tp.Length = length; Clear(); WriteLine("Geometry - Trapezoidal Prism"); WriteLine("------------------------------"); WriteLine("Top Base: {0}", tp.TopBase); WriteLine("Bottom Base: {0}", tp.BottomBase); WriteLine("Height: {0}", tp.Height); WriteLine("Length: {0}", tp.Length); WriteLine("Base Area: " + tp.BaseArea); WriteLine("Top Area: " + tp.TopArea); Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea); Write("Volume: " + tp.Volume + System.Environment.NewLine); WriteLine("=============================="); } } }
Geometry - Trapezoidal Prism ---------------------------- Enter the following values Top Width:
Geometry - Trapezoidal Prism ---------------------------- Enter the following values Top Width: 137.96 Bottom Width: 209.73 Length: 87.59 Height: 142.46
Geometry - Trapezoidal Prism ------------------------------ Top Base: 137.96 Bottom Base: 209.73 Height: 142.46 Length: 87.59 Base Area: 24765.9587 Top Area: 12083.9164 Bottom Area: 18370.2507 Volume: 2169250.322533 ============================== Press any key to continue . . .
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 or the same 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
using System;
namespace Geometry05
{
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 Geometry05
{
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 static System.Console;
namespace Geometry05
{
public class Geometry
{
private static void Main()
{
WriteLine("Geometry - Trapezoidal Prism");
WriteLine("----------------------------");
WriteLine("Enter the following values");
Write("Top Width: ");
double top = double.Parse(ReadLine());
Write("Bottom Width: ");
double bottom = double.Parse(ReadLine());
Write("Length: ");
double length = double.Parse(ReadLine());
Write("Height: ");
double height = double.Parse(ReadLine());
TrapezoidalPrism tp = new TrapezoidalPrism(top: top, bottom: bottom, height: height, length: length);
Clear();
WriteLine("Geometry - Trapezoidal Prism");
WriteLine("------------------------------");
WriteLine("Top Base: {0}", tp.TopBase);
WriteLine("Bottom Base: {0}", tp.BottomBase);
WriteLine("Height: {0}", tp.Height);
WriteLine("Length: {0}", tp.Length);
WriteLine("Base Area: " + tp.BaseArea);
WriteLine("Top Area: " + tp.TopArea);
Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea);
Write("Volume: " + tp.Volume + System.Environment.NewLine);
WriteLine("==============================");
}
}
}
Geometry - Trapezoidal Prism ---------------------------- Enter the following values Top Width: 226.93 Bottom Width: 316.41 Length: 109.86 Height: 186.77
Geometry - Trapezoidal Prism ------------------------------ Top Base: 226.93 Bottom Base: 316.41 Height: 186.77 Length: 109.86 Base Area: 50739.8059 Top Area: 24930.5298 Bottom Area: 34760.8026 Volume: 5574275.076174 ============================== Press any key to continue . . .
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 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
using System; namespace Geometry05 { 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; } } } }
Geometry - Trapezoidal Prism ---------------------------- Enter the following values Top Width: 88.68 Bottom Width: 124.83 Length: 73.97 Height: 214.86
Geometry - Trapezoidal Prism ------------------------------ Top Base: 88.68 Bottom Base: 124.83 Height: 214.86 Length: 73.97 Base Area: 22937.3793 Top Area: 6559.6596 Bottom Area: 9233.6751 Volume: 1696677.946821 ============================== Press any key to continue . . .
using static System.Math; namespace Geometry06 { 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; } }
using static System.Console; namespace Geometry06 { public class Geometry { private static void Main() { Cylinder body = new Cylinder(); WriteLine("Geometric Shapes - Cylinder"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Radius: "); body.Radius = double.Parse(ReadLine()); Write("Length: "); body.Length = double.Parse(ReadLine()); Clear(); WriteLine("Geometry - Cylinder"); WriteLine("---------------------------------"); WriteLine("Radius: {0}", body.Radius); WriteLine("Length: {0}", body.Length); WriteLine("Diameter: " + body.Diameter); WriteLine("Circumference: " + body.Circumference); Write("Cross Area: " + body.CrossArea + System.Environment.NewLine); WriteLine("Lateral Area: " + body.LateralArea); Write("Central Volume: " + body.CentralVolume + System.Environment.NewLine); WriteLine("================================="); } } }
Geometric Shapes - Cylinder ---------------------------- Enter the following values Radius:
Geometric Shapes - Cylinder ---------------------------- Enter the following values Radius: 79.84 Length: 258.93
Geometry - Cylinder --------------------------------- Radius: 79.84 Length: 258.93 Diameter: 159.68 Circumference: 501.649514925218 Cross Area: 20025.8486358147 Lateral Area: 129892.108899587 Central Volume: 5185292.9872715 ================================= Press any key to continue . . .
using static System.Math; namespace Geometry06 { 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); } } } }
using static System.Console; namespace Geometry06 { public class Geometry { private static void Main() { Tank whole = new Tank(); WriteLine("Geometric Volume - Tank"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Radius: "); whole.Radius = double.Parse(ReadLine()); Write("Length: "); whole.Length = double.Parse(ReadLine()); Clear(); WriteLine("Geometric Volume - Tank"); WriteLine("---------------------------------"); WriteLine("Radius: {0}", whole.Radius); WriteLine("Cylinder Length: {0}", whole.Length); WriteLine("---------------------------------"); WriteLine("Each Side (Half Sphere)"); WriteLine("Diameter: " + whole.Diameter); WriteLine("Circumference: " + whole.Circumference); Write("Cross Area: " + whole.CrossArea + System.Environment.NewLine); WriteLine("---------------------------------"); WriteLine("The Central Cylinder"); WriteLine("Lateral Area: " + whole.LateralArea); Write ("Central Volume: " + whole.CentralVolume + System.Environment.NewLine); WriteLine("---------------------------------"); WriteLine("The Tank as a Whole"); WriteLine("Through Width: " + whole.Width); WriteLine("Total Length: " + whole.TotalLength); WriteLine("Total Area: " + whole.TotalArea); WriteLine("Total Volume: " + whole.TotalVolume); WriteLine("================================="); } } }
Geometric Volume - Tank ---------------------------- Enter the following values Radius:
Geometric Volume - Tank ---------------------------- Enter the following values Radius: 79.84 Length: 258.93
Geometric Volume - Tank --------------------------------- Radius: 79.84 Cylinder Length: 258.93 --------------------------------- Each Side (Half Sphere) Diameter: 159.68 Circumference: 501.649514925218 Cross Area: 20025.8486358147 --------------------------------- The Central Cylinder Lateral Area: 129892.108899587 Central Volume: 5185292.9872715 --------------------------------- The Tank as a Whole Through Width: 159.68 Total Length: 418.61 Total Area: 209995.503442846 Total Volume: 7317111.32738276 ================================= Press any key to continue . . .
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
namespace Geometry07
{
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 static System.Math; namespace Geometry07 { 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 * 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 static System.Math;
namespace Geometry07
{
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;
}
}
}
using static System.Console; namespace Geometry07 { public class Geometry { private static void Main() { Cone dongle = new Cone(0.00, 0.00); WriteLine("Geometric Volume - Cone"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Radius: "); double radius = double.Parse(ReadLine()); Write("Height: "); double height = double.Parse(ReadLine()); Circle round = new Circle(radius); dongle = new Cone(radius, height); Circle c = dongle.Create(); Clear(); WriteLine("Geometric Volume - Cone"); WriteLine("---------------------------------"); WriteLine("Base Radius: {0}", radius); WriteLine("Height: {0}", height); WriteLine("---------------------------------"); WriteLine("Base"); WriteLine("Diameter: " + c.Diameter); WriteLine("Circumference: " + c.Circumference); Write("Base Area: " + c.Area + System.Environment.NewLine); WriteLine("---------------------------------"); WriteLine("Cone"); WriteLine("Volume: " + dongle.Volume); WriteLine("================================="); } } }
Geometric Volume - Cone ---------------------------- Enter the following values Radius:
Geometric Volume - Cone ---------------------------- Enter the following values Radius: 84.79 Height: 375.67
Geometric Volume - Cone --------------------------------- Base Radius: 84.79 Height: 375.67 --------------------------------- Base Diameter: 169.58 Circumference: 532.751282195757 Base Area: 22585.9906086891 --------------------------------- Cone Volume: 2828293.03065542 ================================= Press any key to continue . . .
using static System.Console; namespace Geometry07 { public class Geometry { private static void Main() { Cone fancy = new Cone(0.00, 0.00); WriteLine("Geometric Volume - Cone"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Radius: "); double radius = double.Parse(ReadLine()); Write("Height: "); double height = double.Parse(ReadLine()); Cone dongle = new Cone(radius, height); fancy = dongle.Surround(); Clear(); WriteLine("Geometric Volume - Cone"); WriteLine("---------------------------------"); WriteLine("Base Radius: {0}", radius); WriteLine("Height: {0}", height); WriteLine("---------------------------------"); WriteLine("Base"); WriteLine("Diameter: " + fancy.Diameter); WriteLine("Circumference: " + fancy.Circumference); Write("Base Area: " + fancy.Area + System.Environment.NewLine); WriteLine("---------------------------------"); WriteLine("Cone"); WriteLine("Volume: " + fancy.Volume); WriteLine("================================="); } } }
Geometric Volume - Cone --------------------------------- Base Radius: 84.79 Height: 375.67 --------------------------------- Base Diameter: 169.58 Circumference: 532.751282195757 Base Area: 22585.9906086891 --------------------------------- Cone Volume: 2828293.03065542 ================================= Press any key to continue . . .
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.
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
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 Geometry05
{
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
{
// "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 the parent
return base.Area;
}
}
public double TopArea
{
get
{
// "base" refers to the parent
// "this" Length refers to a local property
return this.TopBase * this.Length;
}
}
public double BottomArea
{
get
{
// "base" refers to the parent
// "this" Length refers to a local property
return this.BottomBase * this.Length;
}
}
public new double Area
{
get
{
return BaseArea + TopArea + BottomArea + BaseArea;
}
}
public double Volume
{
get
{
// "base" refers to the parent
// "this" Length refers to a local property
return base.Area * this.Length;
}
}
}
}
using static System.Console; namespace Geometry05 { public class Geometry { private static void Main() { double top = 0.00; double bottom = 0.00; double height = 0.00; double length = 0.00; TrapezoidalPrism tp = new TrapezoidalPrism(0, 0, 0, 0); WriteLine("Geometry - Trapezoidal Prism"); WriteLine("----------------------------"); WriteLine("Enter the following values"); Write("Top Base: "); top = double.Parse(ReadLine()); Write("Bottom Base: "); bottom = double.Parse(ReadLine()); Write("Length: "); length = double.Parse(ReadLine()); Write("Height: "); height = double.Parse(ReadLine()); tp = new TrapezoidalPrism(top: top, bottom: bottom, height: height, length: length); Clear(); WriteLine("Geometry - Trapezoidal Prism"); WriteLine("------------------------------"); WriteLine("Top Base: {0}", tp.TopBase); WriteLine("Bottom Base: {0}", tp.BottomBase); WriteLine("Height: {0}", tp.Height); WriteLine("Length: {0}", tp.Length); WriteLine("------------------------------"); WriteLine("Base Area: " + tp.BaseArea); WriteLine("Top Area: " + tp.TopArea); Write("Bottom Area: {0}" + System.Environment.NewLine, tp.BottomArea); WriteLine("Total Area: " + tp.Area); Write("Volume: " + tp.Volume + System.Environment.NewLine); WriteLine("=============================="); } } }
eometry - Trapezoidal Prism --------------------------- nter the following values op Base:
Geometry - Trapezoidal Prism ---------------------------- Enter the following values Top Base: 137.96 Bottom Base: 209.73 Length: 87.59 Height: 142.46
Geometry - Trapezoidal Prism ------------------------------ Top Base: 137.96 Bottom Base: 209.73 Height: 142.46 Length: 87.59 ------------------------------ Base Area: 24765.9587 Top Area: 12083.9164 Bottom Area: 18370.2507 Total Area: 79986.0845 Volume: 2169250.322533 ============================== Press any key to continue . . .
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|