Inheriting a Class from Another


In order not to have to duplicate code, C# and other object-oriented languages support code inheritance, by which you can use one class as the base of another class. For example, let's say that your banking Web application requires a Checking class and a Savings class. When authoring these classes you realize that aside from a few fields and functions, they have a number of members in common. It would not be practical to create the Checking class and then copy most of the code to the Savings class. Simply copying the code can result in maintenance headaches ; consider what would happen if after copying the code to five different classes, you discover a bug in one of the functions. Instead of copying and pasting code it is possible to create a class called Account. The Account class would contain all the code that every type of Account would have in common. Then you simply write a Checking class or a Savings class that derives from the Account class. Deriving from a class means that you gain all the functionality of the base class plus you can add functions that are specific to each derived class.

To inherit a class from another:

  1. Add a colon at the end of the class name , before the open curly bracket .

  2. After the colon, add the name of the base class you want to inherit from ( Figure 5.5 ).

    Figure 5.5 Both Checking and Savings receive the MakeDeposit method from Checking.
     class Account {    decimal balance;    void MakeDeposit(decimal amount )    {       balance += amount;    } } class Checking  : Account  {    // Inherits Account functionality } class Savings  : Account  {    // Inherits Account functionality } 

graphics/tick.gif Tips

  • If you don't specify a class you want to inherit from, the class automatically inherits from a system class called System.Object.

  • Inheriting from a class means that you gain all the functionality from that class ( Figure 5.6 ).

    Figure 5.6 Notice that even though we are creating a Savings object, we can still call the methods in the Account class because Savings inherits its functionality from Account.
     class Account {    decimal balance;  void MakeDeposit(decimal amount )  {      balance += amount;    } } class Savings : Account {    // Inherits Account functionality } class Bank {    static void CreateAccounts()    {       Savings s = new Savings();       // MakeDeposit is defined in       // Account       s.  MakeDeposit  (500.00m);    } } 
  • It's legal to assign a variable of the base type to an instance of the derived class. The opposite isn't trueyou can't assign a variable of the derived type to an instance of the base class ( Figure 5.7 ).

    Figure 5.7 Whenever you have a base-child class relationship, you can always declare a variable using the base type and assign it to an instance of the child class. The opposite isn't true. You can't have a variable of the child class be equal to an instance of the base class.
     class Account {    decimal balance;    void MakeDeposit(decimal amount )    {       balance += amount;    } } class Savings : Account {    // Inherits Account functionality } class Bank {    static void CreateAccounts()    {       Savings s = new Savings();       // it's OK to do this       Account  a = s  ;       a = new Account();       // it's NOT OK to do this  s = a  ; // won't compile    } } 
  • Inheritance is used for a mechanism known as refactoring, in which you find all the code you have in common in related types and create a base class and move all the common code to it. For example, you start with Savings and Checking. You discover the code that you have in common between the two, then create an Account class and move the common code to Account ( Figure 5.8 ).

    Figure 5.8 With refactoring, you find all the code that Checking and Savings have in common and move it to a class like Account, then derive Checking and Savings from the Account class.
     //**************************** //********* BEFORE *********** //**************************** class Checking {    public void  MakeWithdrawal  (                decimal amount)    {      if(balance >= amount)             balance -= amount;      else         throw new         InvalidOperationException(         "Not enough Money");    } } class Savings {    public void  MakeWithdrawal  (               decimal amount)    {       if(balance >= amount)              balance -= amount;       else          throw new          InvalidOperationException(          "Not enough Money");    } } //**************************** //********* AFTER ************ //**************************** class Account {    public void  MakeWithdrawal  (                decimal amount)    {       if(balance >= amount)               balance -= amount;       else          throw new          InvalidOperationException(          "Not enough Money");    } } class Checking  : Account  { } class Savings  : Account  { } 



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