Tuesday, March 16, 2010

Calling Members of the Base class

Sometimes when we inherit a class, the methods in the derived class can override methods in the base class. What if we wanted to call one of the overridden methods from within the derived class? The keyword base is the solution to this problem. To call an overridden method, such as GetInformation, we can call it by using this statement:
Base.GetInformation();
This statement calls the original GetInformation method, which was overridden by a method with a similar name in the derived class.
Note: - We cannot use the keyword base inside a static method.

In other cases, we need to build the object of the base class before we build the object of the derived class. We can also do that using the base keyword like the following example:

public MyDerived(int x) : base(x)
{
    //…
}
This is the constructor of the class MyDerived, which builds the base class object on the fly. We need to do this in cases when constructing the base class is necessary to construct the derived class. For example, let’s say the base class represents the area of a circle, and the derived class represents an area of a sphere (which uses the area of a circle). In this case, we must instantiate the area of the circle before we can instantiate the area of the sphere.
Example: -
//Example 6-1
//Virtual Example
using System;

//The base class:
public class AreasClass
{
    //Fields:
    protected double x,y;

    //Constructors:
    public AreaClass()
    {
    }
    public AreaClass(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
    //Methods:
    public virtual double Area()
    {
        return 0;
    }
   
}
//The point class uses a parameter less constructor:
class  Point: AreasClass
{
    public Point() : base()
    {
    }
}
//The Circle class:
class Circle: AreasClass
{
    public Circle(double r) : base (r,0)
    {
    }
    public override double Area()
    {
        //The area of a circle.
        //The radius is represented by x:
        return Math.PI *  x * x;
    }
}
//The Sphere class:
class Sphere: AreasClass
{
    public Sphere(double r): base(r,0)
    {
    }
    public override double Area()
    {
        //The radius is represented by x:
        return 4 * Math.PI * x * x;
    }
}
//The Cylinder class:
class Cylinder: AreaClass
{
    public Cylinder(double r, double h) : base (r, h)
    {
    }
    public override double Area()
    {
        //The radius is represented by x and
        //the height is represented by y:
        return 2 * Math.PI * x * x + 2 * Math.PI * x * y;
    }
}
class MyClass
{
    public static void Main()
    {
    //Receive number from the keyboard:
    Console.WriteLine(“Please enter the radius: “);
    double radius= Convert.ToDouble(Console.ReadLine());
    Console.WriteLine(“Please enter the height: “);
    double height = Convert.ToDouble(Console.ReadLine());

    //Create objects:
    Point myPoint = new Point();
    Circle myCircle = new Circle(radius);
    Sphere myShpere = new Sphere(radius);
    Cylinder myCylinder = new Cylinder(radius, height);
    //Display results:
    Console.WriteLine(“Area of our point = {0:F2}”, myPoint.Area());
    Console.WriteLine(“Area of our circle ={0:F2}”, myCircle.Area());
    Console.WriteLine(“Area of our sphere = {0:F2}”, myShpere.Area());
    Console.WriteLine(“Area of our cylinder = {0:F2}”, myCylinder.Area());
    }
}

No comments:

Post a Comment