Invoking Base Constructors


Suppose you write a class called Account to be a base class, and then write a class called Checking and have Checking derive from Account. When a developer creates a new Checking class the compiler invokes the constructor for Checking, the derived class. What about the constructor for Account, the base class?

When you derive a class from another class, the derived class inherits all the non-private functions of the base class, as well as all non-private fields. However, the functions in the base class may have been written assuming that certain fields were initialized prior to their execution.

For that reason it's important for the compiler to also execute the code in the base constructor when the developer creates an instance of the derived class. So the compiler does a little trick: It inserts a line to every constructor that invokes the constructor of the base class automatically. The problem is that this trick is limitedthe compiler can only invoke the default constructor of the base. If the base class doesn't have a default constructor, or if you want to invoke a different constructor than the default, you must do it yourself with code.

To invoke a base constructor:

  1. Add a constructor to the derived class. It can be a default constructor or a parameterized constructor.

  2. Before the curly brackets after the closing parenthesis of the constructor's parameters, add a colon followed by a space.

  3. Type base , in lowercase, followed by the parameters for the base constructor you want to invoke in parentheses ( Figure 6.12 ).

    Figure 6.12 Checking is derived from Account, but Account has a single constructor that requires a parameter. You need to call the constructor explicitly in Checking by adding a constructor to it and using base to pass a parameter to Account's constructor.
     class Account {    public Account(int InitialBalance)    {    //code omitted for simplicity    } } class Checking : Account {    public Checking() :  base(100)  {    }    public Checking(int InitialBalance) :  base(InitialBalance)  {    } } 

graphics/tick.gif Tips

  • Use this technique when the base class doesn't have a default constructor (a constructor that takes no parameters), or when you want to invoke a different constructor.

  • When using the base keyword you can specify in parentheses a literal value or simply use one of the parameters sent in to the constructor. In Figure 6.12 , the second constructor in Checking has a parameter called InitialBalance and forwards this parameter in the base function.

  • You can't use a field or specify a function of the class as the parameter for the base function unless the field or function is marked as static (see "Building Code Libraries with Static Members," later in this chapter). If the member isn't marked as static, the compiler will issue an error.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net