Generics and Inheritance
Generics and Inheritance
Generic Classes and Inheritance
Introduction
Consider the following geometric figures:
![]() |
![]() |
![]() |
![]() |
Square | Rectangle | Trapezoid | Parallelogram |
Notice that these are geometric figures with each having four sides. From what we know so far, we can create a base class to prepare it for inheritance. If the class is very general, we can make it a generic one. We can set a data type as an unknown type, anticipating that the dimensions of the figure can be considered as integer or double-precision types. Here is an example:
public class Quadrilateral<T> { protected T _base; protected T _height; protected string? _name; public virtual T Base { get { return _base; } set { _base = value; } } public virtual T Height { get { return _height; } set { _height = value; } } public virtual string? Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual string ShowCharacteristics() { return $"Geometric Figure: {Name}, Description: {Describe()}, Base: {Base}, Height: {Height}"; } }
You can then use the class by declaring a variable of it. Here are examples:
using static System.Console; Title = "Geometry: Quadrilaterals"; WriteLine("Geometry: Quadrilaterals"); WriteLine("--------------------------------"); // Trapezoid with equal sides var kite = new Quadrilateral<double>("Beach Kite", 18.64, 18.64); WriteLine(kite.ShowCharacteristics()); // Rectangle, in meters var basketballStadium = new Quadrilateral<Byte>(); basketballStadium.Name = "Basketball Stadium"; basketballStadium.Base = 15; basketballStadium.Height = 28; WriteLine(basketballStadium.ShowCharacteristics()); WriteLine("================================="); public class Quadrilateral<T> { protected T _base; protected T _height; protected string? _name; public virtual T Base { get { return _base; } set { _base = value; } } public virtual T Height { get { return _height; } set { _height = value; } } public virtual string? Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual string ShowCharacteristics() { return $"Geometric Figure: {Name}, Description: {Describe()}, Base: {Base}, Height: {Height}"; } }
This would produce:
Geometry: Quadrilaterals -------------------------------- Geometric Figure: Beach Kite, Description: A quadrilateral is a geometric figure with four sides, Base: 18.64, Height: 18.64 Geometric Figure: Basketball Stadium, Description: A quadrilateral is a geometri c figure with four sides, Base: 15, Height: 28 ================================= Press any key to close this window . . .
The Nullity of a Generic Parameter
We have seen that, when you are creating a generic class, the compiler doesn't know the type of the parameter you are using. This means that the generic parameter can be a reference type. This also means that the values or objects of the parameter could eventually hold null values. This would cause the compiler to issue a warning on a field or property created from the generic parameter. To prepare for this, you can apply the null-conditional operator on every field or property created from the parameter type. This can be done as follows:
public class Quadrilateral<T> { protected T? _base; protected T? _height; protected string? _name; public virtual T? Base { get { return _base; } set { _base = value; } } public virtual T? Height { get { return _height; } set { _height = value; } } public virtual string? Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual string ShowCharacteristics() { return $"Geometric Figure: {Name}, Description: {Describe()}, Base: {Base}, Height: {Height}"; } }
Deriving from a Generic Class
If you have a generic class that can serve as a foundation for another class, you can derive a class from the generic one. To do this, use the formula we apply when deriving a class but follow the name of each class with <>. Inside the <> operator, enter the same identifier to indicate that the class is a generic type that is based on another generic class. Here is an example:
public class Square<T> : Quadrilateral<T> { }
In the body of the new class, you can use the parameter type. For example, you can declare some member variables of that type. You can create methods that return the parameter type or you can pass arguments of the parameter type. When implementing the methods of the new class, use the member variables of the parameter and the argument(s) based on the parameter type. You can then declare a variable of the class and use it as we have done so far for other generic classes. Here is an example:
public class Quadrilateral<T> { protected T? _base; protected T? _height; protected string? _name; public virtual T? Base { get { return _base; } set { _base = value; } } public virtual T? Height { get { return _height; } set { _height = value; } } public virtual string? Name { get { return _name; } set { _name = value; } } public Quadrilateral(string name = "Quadrilateral") { _name = name; } public Quadrilateral(T bs, T height) { _name = "Quadrilateral"; _base = bs; _height = height; } public Quadrilateral(string name, T bs, T height) { _name = name; _base = bs; _height = height; } public virtual string Describe() { return "A quadrilateral is a geometric figure with four sides"; } public virtual string ShowCharacteristics() { return $"Geometric Figure: {Name}, Description: {Describe()}, Base: {Base}, Height: {Height}"; } } public class Square<T> : Quadrilateral<T> { public Square() { _name = "Square"; } public Square(string name) { _name = "Square"; } public Square(T side) { _name = "Square"; _base = side; _height = side; } public Square(string name, T side) { _name = name; _base = side; _height = side; } public override string Describe() { return "A square is a quadrilateral with four equal sides"; } public override string ShowCharacteristics() { return $"Geometric Figure: {Name}, Description: {Describe()}. {Describe()}, Side: {Base}"; } }
You can then use the class. Here is an example:
using static System.Console;
Title = "Geometry: Quadrilaterals";
WriteLine("Geometry: Quadrilaterals");
WriteLine("--------------------------------");
var plate = new Square<Byte>();
plate.Name = "Plate";
plate.Base = 15;
plate.Height = 28;
WriteLine(plate.ShowCharacteristics());
WriteLine("=================================");
public class Quadrilateral<T>
{
protected T? _base;
protected T? _height;
protected string? _name;
public virtual T? Base
{
get { return _base; }
set { _base = value; }
}
public virtual T? Height
{
get { return _height; }
set { _height = value; }
}
public virtual string? Name
{
get { return _name; }
set { _name = value; }
}
public Quadrilateral(string name = "Quadrilateral") => _name = name;
public Quadrilateral(T bs, T height)
{
_name = "Quadrilateral";
_base = bs;
_height = height;
}
public Quadrilateral(string name, T bs, T height)
{
_name = name;
_base = bs;
_height = height;
}
public virtual string Describe() => "A quadrilateral is a geometric figure with four sides";
public virtual string ShowCharacteristics() => $"Geometric Figure: {Name}, Description: {Describe()}, Base: {Base}, Height: {Height}";
}
public class Square<T> : Quadrilateral<T>
{
public Square()
{
_name = "Square";
}
public Square(string name)
{
_name = "Square";
}
public Square(T side)
{
_name = "Square";
_base = side;
_height = side;
}
public Square(string name, T side)
{
_name = name;
_base = side;
_height = side;
}
public override string Describe()
{
return "A square is a quadrilateral with four equal sides";
}
public override string ShowCharacteristics()
{
return $"Geometric Figure: {Name}, Description: {Describe()}. {Describe()}, Side: {Base}";
}
}
This would produce:
Geometry: Quadrilaterals -------------------------------- Geometric Figure: Plate, Description: A square is a quadrilateral with four equa l sides. A square is a quadrilateral with four equal sides, Side: 15 ================================= Press any key to close this window . . .
A Generic Interface
Introduction
If you are planning to create many generic classes, you can start or provide their common characteristics or behaviors in an interface.
Creating a Generic Interface
To create a generic interface, you primarily follow the rules for creating an interface except that you must add a parameter type. Here is an example:
public interface ICounter<T>
{
}
You should also add a (the) member(s) that the implementers will have to override. Here are examples of two members:
public interface ICounter<T>
{
int Count { get; }
T Get(int index);
}
In the same way, you can derive a generic interface from another generic interface. Here is an example:
public interface ICounter<T> { int Count { get; } T Get(int index); } public interface IBuilder<T> : ICounter<T> { void Add(T item); }
Implementing a Generic Interface
After creating a generic interface, when deriving a class from it, follow the formula we reviewed for inheriting from a generic class. This means that the deriving class must use (a) parameter type(s). Here is an example:
public interface ICounter<T>
{
int Count { get; }
T Get(int index);
}
public interface IBuilder<T> : ICounter<T>
{
void Add(T item);
}
public class People<T> : IPersons<T>
{
}
When implementing the derived class, you must observe all rules that apply to interface implementation. That is, you must implement all the members of the generic interface. Of course, you can also add new members if you want. Here is an example:
public interface ICounter<T> { int Count { get; } T Get(int index); } public interface IBuilder<T> : ICounter<T> { void Add(T item); } public class Associate<T> : IBuilder<T> { private int size; private T[] objects; public Associate() { size = 0; objects = new T[10]; } // This is a property implemented from the ICounter interface public int Count { get { return size; } } // This is a method implemented from the IBuilder interface public void Add(T pers) { objects[size] = pers; size++; } // This is a method implemented from the ICounter interface public T Get(int index) { return objects[index]; } }
After implementing the interface, you can declare a variable of the class and use it as you see fit. To do this, after the nane of the class, make sure you specify the parameter type between < and >. Initialize the variable appropriately. After that, you can acccess the members of the class. Here is an example:
using static System.Console; Title = "Social Anthropology"; WriteLine("Social Anthropology"); WriteLine("------------------------------------------"); Associate<string> anthropology = new Associate<string>(); anthropology.Add("Convention"); anthropology.Add("Economics"); anthropology.Add("Politics"); anthropology.Add("Social Conflict"); anthropology.Add("Consumption"); WriteLine("The study of social anthropology includes:"); for (int i = 0; i < anthropology.Count; i++) WriteLine(anthropology.Get(i)); WriteLine("=========================================="); public interface ICounter<T> { int Count { get; } T Get(int index); } public interface IBuilder<T> : ICounter<T> { void Add(T item); } public class Associate<T> : IBuilder<T> { private int size; private T[] objects; public Associate() { size = 0; objects = new T[10]; } // This is a property implemented from the ICounter interface public int Count { get { return size; } } // This is a method implemented from the IBuilder interface public void Add(T pers) { objects[size] = pers; size++; } // This is a method implemented from the ICounter interface public T Get(int index) { return objects[index]; } }
This would produce:
Social Anthropology ------------------------------------------ The study of social anthropology includes: Convention Economics Politics Social Conflict Consumption ========================================== Press any key to close this window . . .
In the above example, we used a primitive type, namely a string, as a parameter type. Otherwise, you can use a class, either one of the many .NET Framework built-in classes or you can create your own.
Remember that you can declare the variable using either the var or the dynamic keyword. Here are examples:
using static System.Console;
var anthropology = new Associate<string>();
dynamic salaries = new Associate<decimal>();
anthropology.Add("Convention");
anthropology.Add("Economics");
anthropology.Add("Politics");
anthropology.Add("Social Conflict");
anthropology.Add("Consumption");
for(int i = 0; i < anthropology.Count; i++)
anthropology.Get(i);
A Generic Interface as Parameter
A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. Here is an example:
using static System.Console;
Title = "Social Anthropology";
WriteLine("Social Anthropology");
WriteLine("------------------------------------------");
IBuilder<string> anthropology = new Associate<string>();
anthropology.Add("Convention");
anthropology.Add("Economics");
anthropology.Add("Politics");
anthropology.Add("Social Conflict");
anthropology.Add("Consumption");
WriteLine("The study of social anthropology includes:");
for (int i = 0; i < anthropology.Count; i++)
WriteLine(anthropology.Get(i));
WriteLine("==========================================");
In the same way, a generic interface can be returned from a method. Here is an example:
public interface ICounter<T> { int Count { get; } T Get(int index); } public interface IBuilder<T> : ICounter<T> { void Add(T item); } public class Associate<T> : IBuilder<T> { private int size; private T[] objects; public Associate() { size = 0; objects = new T[10]; } public int Count { get { return size; } } public void Add(T pers) { objects[size] = pers; size++; } public T Get(int index) { return objects[index]; } } public class College { public IBuilder<Course> CreateCourses() { Associate<Course> courses = new Associate<Course>(); Course crs = new Course(); crs.CourseName = "Online Research"; crs.Credits = 1; courses.Add(crs); crs = new Course() { CourseName = "General Chemistry", Credits = 3 }; courses.Add(crs); courses.Add(new Course() { CourseName = "Workplace Learning in Biology", Credits = 6 }); courses.Add(new Course() { CourseName = "Linear Geometry 1", Credits = 4 }); return courses; } } public record Course { public string? CourseName { get; set; } public int Credits { get; set; } }
Here is an example of getting the returned generic value:
using static System.Console;
Title = "Social Anthropology";
WriteLine("Social Anthropology");
WriteLine("------------------------------------------");
College university = new College();
IBuilder<Course> studies = university.CreateCourses();
for (int i = 0; i < studies.Count; i++)
WriteLine("{0}: {1} Credits", studies.Get(i).CourseName, studies.Get(i).Credits);
WriteLine("==========================================");
public interface ICounter<T>
{
int Count { get; }
T Get(int index);
}
public interface IBuilder<T> : ICounter<T>
{
void Add(T item);
}
public class Associate<T> : IBuilder<T>
{
private int size;
private T[] objects;
public Associate()
{
size = 0;
objects = new T[10];
}
public int Count { get { return size; } }
public void Add(T pers)
{
objects[size] = pers;
size++;
}
public T Get(int index) { return objects[index]; }
}
public class College
{
public IBuilder<Course> CreateCourses()
{
Associate<Course> courses = new Associate<Course>();
Course crs = new Course();
crs.CourseName = "Online Research";
crs.Credits = 1;
courses.Add(crs);
crs = new Course() { CourseName = "General Chemistry", Credits = 3 };
courses.Add(crs);
courses.Add(new Course() { CourseName = "Workplace Learning in Biology", Credits = 6 });
courses.Add(new Course() { CourseName = "Linear Geometry 1", Credits = 4 });
return courses;
}
}
public record Course
{
public string? CourseName { get; set; }
public int Credits { get; set; }
}
This would produce:
Social Anthropology ------------------------------------------ Online Research: 1 Credits General Chemistry: 3 Credits Workplace Learning in Biology: 6 Credits Linear Geometry 1: 4 Credits ========================================== Press any key to close this window . . .
A generic interface can also be passed as argument. You pass a generic interface primarily the same way you would a regular interface. In the body of the method, you can ignore the argument or use it any way appropriate. Here is an example:
public class College { public IBuilder<Course> CreateCourses() { Associate<Course> courses = new Associate<Course>(); Course crs = new Course(); crs.CourseName = "Online Research"; crs.Credits = 1; courses.Add(crs); crs = new Course() { CourseName = "General Chemistry", Credits = 3 }; courses.Add(crs); courses.Add(new Course() { CourseName = "Workplace Learning in Biology", Credits = 6 }); courses.Add(new Course() { CourseName = "Linear Geometry 1", Credits = 4 }); return courses; } public string Show(IBuilder<Course> values) { string strCourses = ""; for (int i = 0; i <= 4; i++) { strCourses += values.Get(i).CourseName + "(" + values.Get(i).Credits + "), "; } strCourses = strCourses.Substring(0, strCourses.Length - 2); return strCourses; } } public record Course { public string? CourseName { get; set; } public int Credits { get; set; } }
Here is an example of passing the generic object as argument:
using static System.Console; Title = "Anthropology - College Courses"; WriteLine("Anthropology - College Courses"); WriteLine("---------------------------------------------------------------"); College university = new College(); IBuilder<Course> studies = new Associate<Course>(); studies.Add(new Course() { CourseName = "Business Startup", Credits = 1 }); studies.Add(new Course() { CourseName = "Introduction to Business and Management", Credits = 3 }); studies.Add(new Course() { CourseName = "Fundamentals of Digital Media", Credits = 3 }); studies.Add(new Course() { CourseName = "Predictive Modeling", Credits = 6 }); studies.Add(new Course() { CourseName = "Financial Management for Health Care Organizations", Credits = 3 }); string summary = university.Show(studies); WriteLine(summary); WriteLine("==============================================================="); public interface ICounter<T> { int Count { get; } T Get(int index); } public interface IBuilder<T> : ICounter<T> { void Add(T item); } public class Associate<T> : IBuilder<T> { private int size; private T[] objects; public Associate() { size = 0; objects = new T[10]; } public int Count { get { return size; } } public void Add(T pers) { objects[size] = pers; size++; } public T Get(int index) { return objects[index]; } } public class College { public IBuilder<Course> CreateCourses() { Associate<Course> courses = new Associate<Course>(); Course crs = new Course(); crs.CourseName = "Online Research"; crs.Credits = 1; courses.Add(crs); crs = new Course() { CourseName = "General Chemistry", Credits = 3 }; courses.Add(crs); courses.Add(new Course() { CourseName = "Workplace Learning in Biology", Credits = 6 }); courses.Add(new Course() { CourseName = "Linear Geometry 1", Credits = 4 }); return courses; } public string Show(IBuilder<Course> values) { string strCourses = string.Empty; for (int i = 0; i <= 4; i++) strCourses += values.Get(i).CourseName + " (" + values.Get(i).Credits + " Credits)\n"; strCourses = strCourses.Substring(0, strCourses.Length - 1); return strCourses; } } public record Course { public string? CourseName { get; set; } public int Credits { get; set; } }
This would produce:
Anthropology - College Courses --------------------------------------------------------------- Business Startup (1 Credits) Introduction to Business and Management (3 Credits) Fundamentals of Digital Media (3 Credits) Predictive Modeling (6 Credits) Financial Management for Health Care Organizations (3 Credits) =============================================================== Press any key to close this window . . .
A Generic Interface as a Parameter Type
A generic interface can be used as a parameter type. When creating a method, in its <> operator, specify the desired interface. A generic interface can also be used as the parameter type of an interface. As the number one rule for all methods that return a value, before exiting the method, you must return an object that is compatible with the generic interface. To do this, in the body of the method, you can declare a variable of a class that implements the interface, use that variable any appropriate way you want, and return it. Here is an example:
public interface ICounter<T>
{
int Count { get; }
T Get(int index);
}
public interface IBuilder<T> : ICounter<T>
{
void Add(T item);
}
public interface IGovernment
{
string? Continent { get; set; }
string? Name { get; set; }
}
public class Country : IGovernment
{
public string? Continent { get; set; }
public string? Name { get; set; }
}
public class Associate<T> : IBuilder<T>
{
private int size;
private T[] objects;
public Associate()
{
size = 0;
objects = new T[10];
}
public int Count { get { return size; } }
public void Add(T pers)
{
objects[size] = pers;
size++;
}
public T Get(int index) { return objects[index]; }
}
public class Politics
{
public Associate<IGovernment> Create()
{
IGovernment gov = new Country();
gov.Continent = "Africa";
gov.Name = "Sénégal";
Associate<IGovernment> country = new Associate<IGovernment>();
country.Add(gov);
return country;
}
}
|
|||
Previous | Copyright © 2008-2025, FunctionX | Tuesday 19 October 2021 | Next |
|