C#. An example of accessing methods and properties of inherited classes using a reference to the base class




An example of accessing methods and properties of inherited classes using a reference to the base class

The example examines:

  • ways to access instances of inherited classes using a reference to the base class;
  • study of the features of the use of keywords is, as;
  • methods for calling methods of the same name in the class hierarchy.

Contents


Search other websites:

Task

Declare the Figure class, which contains the name field defining the name of the figure. In the Figure class, declare the following methods:

  • constructor with 1 parameter;
  • Display() method to display the name of figure.

From the Figure class, inherit the Rectangle class (rectangle) with the following fields:

  • coordinate of the upper left corner (x1; y1);
  • coordinate of the lower right corner (x2; y2).

In the Rectangle class, implement the following methods and functions:

  • a constructor with 5 parameters that calls the constructor of the base class Figure;
  • a constructor without parameters to set the coordinates of the angles (0; 0), (1; 1) and call the constructor with 5 parameters using the this keyword;
  • Display() method that displays the name of the figure and the value of the internal fields. This method refers to the base class method of the same name;
  • method Area(), which returns the area of the rectangle.

From the Rectangle class, you need to inherit the RectangleColor class. In the RectangleColor class, implement the color field and the following methods:

  • a constructor with 6 parameters, which calls the constructor of the base class Rectangle;
  • constructor without parameters, which sets the coordinates (0; 0), (1; 1) and calls the constructor with 6 parameters using the this keyword;
  • method Display() that displays the name of the figure and the values of the internal fields. This method refers to the base class method of the same name;
  • method Area(), which returns the area of the rectangle. The method calls the Area() method of the base class.

In the Main() function, do the following:

    • declare a reference to the base class Figure;
    • create instances of the Rectangle and RectangleColor classes;
  • demonstrate access to methods of derived classes by referencing the Figure class.

 



Instructions. Program text

Below is the solution of the problem. The program is implemented as a Console Application.

using System;
using static System.Console;

namespace ConsoleApp1
{
  // Class Figure
  class Figure
  {
    // protected field of class
    protected string name;

    // constructor with 1 parameter
    public Figure(string name) { this.name = name; }

    // Method Display()
    public void Display()
    {
      WriteLine("Figure.name = {0}", name);
    }
  }

  // Class Rectangle - inherits (extends) possibilities of class Figure
  class Rectangle : Figure
  {
    // Hidden fields - coordinates of fields
    protected double x1, y1, x2, y2;

    // Constructor with 5 parameters
    public Rectangle(string name, double x1, double y1, double x2, double y2) :
        base(name) // call the base class constructor
    {
      this.x1 = x1; this.y1 = y1;
      this.x2 = x2; this.y2 = y2;
    }

    // Constructor without parameters, calls the constructor with 5 parameters
    public Rectangle() : this("Rectangle", 0, 0, 1, 1) { }

    // Method Display() - displays the values of the fields,
    // calls the base class method of the same name.
    // This method overrides the Diplay() method
    // of the base class Figure, so the new keyword is specified
    public new void Display()
    {
      base.Display(); // call the Display() method of the base class
      Write("Rectangle: x1 = {0:f2}, y1 = {1:f2}, ", x1, y2);
      WriteLine("x2 = {0:f2}, y2 = {1:f2}", x2, y2);
    }

    // Area of a rectangle
    public double Area()
    {
      return Math.Abs(x1 - x2) * Math.Abs(y1 - y2);
    }
  }

  // RectangleColor class - adds color to a rectangle,
  // inherits the capabilities of the Rectangle class
  class RectangleColor : Rectangle
  {
    // Hidden class field
    protected int color = 0;

    // Constructor with 6 parameters,
    // calls the Rectangle base class constructor
    public RectangleColor(string name, double x1, double x2,
    double y1, double y2, int color) : base(name, x1, y1, x2, y2)
    {
      this.color = color;
    }

    // Constructor without parameters,
    // calls the constructor with 6 parameters
    public RectangleColor() : this("RectangleColor", 0, 0, 1, 1, 0) { }

    // Method Display() - calls the method of the same name
    // of the base class, in order not to hide it, you need to use the new keyword
    public new void Display()
    {
      base.Display();
      WriteLine("RectangleColor.color = {0}", color);
    }

    // The area of a rectangle
    public new double Area()
    {
      return base.Area(); // invoke the Area() method of the base class
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // 1. Declare a reference to the base class
      Figure refFg;

      // 2. Create instances of classes Figure, Rectangle, Rectangle
      Figure objFg = new Figure("Figure");
      Rectangle objRect = new Rectangle("Rectangle", 1, 2, 5, -4);
      RectangleColor objRectCol = new RectangleColor("RectangleColor", 1, 8, -1, 3, 2);

      // 3. Set refFg to the value of the objFg reference to the base class Figure
      refFg = objFg;
      refFg.Display(); // The Display() method of the Figure class is called.
      WriteLine("-----------------------");

      // 4. Set refg to object
      refFg = objRect;

      // 4.1. The Display() method of the Figure class is called.
      refFg.Display();
      WriteLine("-----------------------");

      // 4.2. The Display() method of the Rectangle class is called - method 1
      try
      {
        ((Rectangle)refFg).Display(); // explicit cast to Rectangle type
        WriteLine("-----------------------");
      }
      catch
      {
        // if casting to the type did not work correctly, then exit
        WriteLine("Error.");
        return;
      }

      // 4.3. The Display() method of the Rectangle class is called - method 2.
      // This method uses the as keyword, which implements
      // an attempt to cast one type to another,
      // in our case Figure => Rectangle
      // declare an additional reference of Rectangle type
      Rectangle r = refFg as Rectangle;

      if (r != null) r.Display();
      WriteLine("-----------------------");

      // 4.4. Method Display() of class Rectangle is called - method 3.
      // The is keyword is used here to determine type compatibility,
      // the result of the check is the bool type.
      // If types can be cast, then true is returned, otherwise false.
      if (refFg is Rectangle)
      {
        (refFg as Rectangle).Display();
        WriteLine("-----------------------");
      }

      // 5. Declare a reference to the instance of  RectangleColor class
      refFg = objRectCol;

      // 5.1. Call the Display() method of the base class Figure (rather than Rectangle)
      refFg.Display();
      WriteLine("-----------------------");

      // 5.2. Call the Display() method of the Rectangle class in one of the ways,
      // described in clauses 4.2 - 4.4.
      try
      {
        ((Rectangle)refFg).Display();
        WriteLine("-----------------------");
      }
      catch
      {
        WriteLine("Error.");
        return;
      }

      // 5.3. Call the Display() method of class RectangleColor in one of the ways,
      // described in clauses 4.2 - 4.4.
      if (refFg is RectangleColor)
      {
        (refFg as RectangleColor).Display();
        WriteLine("-----------------------");
      }
    }
  }
}

 


Related topics

 


 

0
fb-share-icon20