Note: this article is published on 07/23/2024.
These two articles are with similar topics, we make them together:
Question:
This is the similar issue discussed in the previous article, while it is claimed by Microsoft: that except static and instance constructors and finalizer,
- all other members of a base class are inherited by derived classes
then we got the conclusion:
Conclusion:
- static members are inherited by the derived class.
Let us show the result by a code demo
Demo:
This is a sample code still from Microsoft [ref]:
Base class: Shape:
public abstract class Shape
{
public abstract double Area { get; }
public abstract double Perimeter { get; }
public override string ToString() => GetType().Name;
public static double GetArea(Shape shape) => shape.Area;
public static double GetPerimeter(Shape shape) => shape.Perimeter;
}
with two static members:
Three derived classes:
using System;
public class Square : Shape
{
public Square(double length)
{
Side = length;
}
public double Side { get; }
public override double Area => Math.Pow(Side, 2);
public override double Perimeter => Side * 4;
public double Diagonal => Math.Round(Math.Sqrt(2) * Side, 2);
}
public class Rectangle : Shape
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public double Length { get; }
public double Width { get; }
public override double Area => Length * Width;
public override double Perimeter => 2 * Length + 2 * Width;
public bool IsSquare() => Length == Width;
public double Diagonal => Math.Round(Math.Sqrt(Math.Pow(Length, 2) + Math.Pow(Width, 2)), 2);
}
public class Circle : Shape
{
public Circle(double radius)
{
Radius = radius;
}
public override double Area => Math.Round(Math.PI * Math.Pow(Radius, 2), 2);
public override double Perimeter => Math.Round(Math.PI * 2 * Radius, 2);
// Define a circumference, since it's the more familiar term.
public double Circumference => Perimeter;
public double Radius { get; }
public double Diameter => Radius * 2;
}
Main class:
public static void Main()
{
Shape[] shapes = { new Rectangle(10, 12), new Square(5),
new Circle(3) };
foreach (Shape shape in shapes)
{
Console.WriteLine($"{shape}: area, {Rectangle.GetArea(shape)}; --- using Rectanle class " +
$"perimeter, {Circle.GetPerimeter(shape)} --- using Circle class");
if (shape is Rectangle rect)
{
Console.WriteLine($" Is Square: {rect.IsSquare()}, Diagonal: {rect.Diagonal}");
continue;
}
if (shape is Square sq)
{
Console.WriteLine($" Diagonal: {sq.Diagonal}");
continue;
}
}
Console.ReadLine();
}
}
// The example displays the following output:
// Rectangle: area, 120; perimeter, 44
// Is Square: False, Diagonal: 15.62
// Square: area, 25; perimeter, 20
// Diagonal: 7.07
// Circle: area, 28.27; perimeter, 18.85
The code uses static method:
- Shape.GetArea(shape)
- Shape.GetPerimeter(shape)
We replace the base class static methods by the one from derived classes:
- Rectangle.GetArea(shape)
- Circle.GetPerimeter(shape)
The results are the same because both of them are runnig the same static methods.
References: